Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get JSON stringify value

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 ?

like image 919
Antonio Avatar asked Nov 28 '22 16:11

Antonio


2 Answers

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']);
}
like image 148
Yaman Jain Avatar answered Dec 10 '22 00:12

Yaman Jain


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']);
}
like image 45
Creative Learner Avatar answered Dec 10 '22 00:12

Creative Learner