获取JSON字符串值

时间:2017-02-17 06:07:38

标签: javascript json loops stringify

我有这样的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']);
}

我怎么能这样做?

4 个答案:

答案 0 :(得分:7)

此代码仅提取价格详情。

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']);
}

答案 1 :(得分:5)

观察:

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

<强>样本

&#13;
&#13;
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']);
}
&#13;
&#13;
&#13;

答案 2 :(得分:1)

你会像这样

制作一个字符串化的对象
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(对象)解析你的obj 然后应用此循环广告让我知道它得到任何错误 骗这个

var parseObject = JSON.parse(object);

答案 3 :(得分:0)

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

例如:

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

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

一旦字符串化,就无法访​​问数组中的数据