Javascript 获取 JSON 字符串化值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42290571/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get JSON stringify value
提问by Antonio
I have JSON stringify data like this :
我有这样的 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"}]
I want to get only pricevalue of that data. I have tried this way but it doesn't work.
我只想获得该数据的价格值。我已经尝试过这种方式,但它不起作用。
var stringify = JSON.stringify(values);
for(var i = 0; i < stringify.length; i++)
{
alert(stringify[i]['price']);
}
How could I to do that ?
我怎么能那样做?
回答by Yaman Jain
This code will only fetch the price details.
此代码只会获取价格详细信息。
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']);
}
回答by Rohit Jindal
Observation :
观察:
If you want to parse the array of objectsto get the property valueyou have to convert in into JSON objectfirst.
如果您想解析array of objects以获取value您必须先转换为的属性JSON object。
DEMO
演示
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']);
}
回答by Asad
you will geting a stringified object like this
你会得到一个像这样的字符串化对象
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"}]';
parse your obj using JSON.parse(object) then apply this loop ad let me know it get any error lie this
使用 JSON.parse(object) 解析你的 obj 然后应用这个循环广告让我知道它得到任何错误谎言
var parseObject = JSON.parse(object);
回答by Sylvain
instead of using stringifybefore selecting the data you should use your loop directly on the valuesarray.
而不是stringify在选择数据之前使用,您应该直接在values数组上使用循环。
For example :
例如 :
var priceArray = array();
values.forEach (data) {
alert(data['price'];
priceArray.push(data['price']);
}
stringify = JSON.stringify(values);
stringifiedPriceArray = JsON.stringify(priceArray);
Once stringified, you can't reach the data in your array
字符串化后,您将无法访问数组中的数据

