最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Get JSON stringify value - Stack Overflow

programmeradmin0浏览0评论

I have JSON stringify data like this :

[{"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 price value 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 ?

I have JSON stringify data like this :

[{"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 price value 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 ?

Share Improve this question edited Mar 21, 2018 at 9:49 Yaman Jain 1,25111 silver badges17 bronze badges asked Feb 17, 2017 at 6:07 AntonioAntonio 7654 gold badges15 silver badges37 bronze badges 6
  • I stringify the name of your variable ? Or are you refering to the JSON.stringify methode ? And then, is this variable the json string you posted ? – Sylvain Commented Feb 17, 2017 at 6:14
  • @Sylvain var stringify = JSON.stringify(values); I got from that. – Antonio Commented Feb 17, 2017 at 6:15
  • wat error u getting its working fine for me in console of chrome – Asad Commented Feb 17, 2017 at 6:16
  • @Asad I got undefined in alert box. – Antonio Commented Feb 17, 2017 at 6:17
  • refer to my naswer @Antonio – Asad Commented Feb 17, 2017 at 6:19
 |  Show 1 more comment

4 Answers 4

Reset to default 21

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

Observation :

If you want to parse the array of objects to get the property value you have to convert in into JSON object first.

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

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

var parseObject = JSON.parse(object);

instead of using stringify before selecting the data you should use your loop directly on the values array.

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

发布评论

评论列表(0)

  1. 暂无评论