获取 JSON 字符串化值

Get JSON stringify value

我有 JSON 像这样对数据进行字符串化:

[{"availability_id":"109465","date":"2017-02-21","price":"430000"},{"availability_id":"109466","date":"2017-02-22","price":"430000"},{"availability_id":"109467","date":"2017-02-23","price":"430000"}]

我只想获得该数据的 价格 值。这个方法我试过了,还是不行。

var stringify = JSON.stringify(values);

for(var i = 0; i < stringify.length; i++)
{
    alert(stringify[i]['price']);
}

我该怎么做?

此代码只会获取价格详情。

var obj = '[{"availability_id":"109465","date":"2017-02-21","price":"430000"},{"availability_id":"109466","date":"2017-02-22","price":"430000"},{"availability_id":"109467","date":"2017-02-23","price":"430000"}]';
var stringify = JSON.parse(obj);
for (var i = 0; i < stringify.length; i++) {
    console.log(stringify[i]['price']);
}

你会得到一个像这样的字符串化对象

var obj='[{"availability_id":"109465","date":"2017-02-21","price":"430000"},
{"availability_id":"109466","date":"2017-02-22","price":"430000"},
{"availability_id":"109467","date":"2017-02-23","price":"430000"}]';

使用 JSON.parse(object) 解析你的 obj 然后应用这个循环广告让我知道它有任何错误 撒谎

var parseObject = JSON.parse(object);

观察:

如果要解析 array of objects 以获得 属性 value,则必须先将其转换为 JSON object

演示

var jsonStringify = '[{"availability_id":"109465","date":"2017-02-21","price":"430000"},{"availability_id":"109466","date":"2017-02-22","price":"430000"},{"availability_id":"109467","date":"2017-02-23","price":"430000"}]';

var jsonObj = JSON.parse(jsonStringify);

for(var i = 0; i < jsonObj.length; i++)
{
    alert(jsonObj[i]['price']);
}

不要在选择数据之前使用 stringify,而应直接在 values 数组上使用循环。

例如:

var priceArray = array();
values.forEach (data) {
    alert(data['price'];
    priceArray.push(data['price']);
}

stringify = JSON.stringify(values);
stringifiedPriceArray = JsON.stringify(priceArray);

字符串化后,您将无法访问数组中的数据