I need to parse this json with javascript
{
"MasterProducts": {
"MasterProduct": [
{
"Productcode": "0903-000192",
"Description": "ICROCOMPUTER;32FX200 24BIT QFP 132P",
"ThumbPic": "NoImage.png",
"RRP": "41.400000",
"Stock": "0"
},
{
"Productcode": "0160-030",
"Description": "AXIS MPEG-4 Decoder 50-user licence pack onto 50 separate computers. For all Axis MPEG-4 products that do not support AAC audio encoding. (210 211 210A 211A 213 214 221 225FD 231D+ 232D+ 241S 241Q 241SA 241QA 242SIV 243SA 282 282A).",
"ThumbPic": "NoImage.png",
"RRP": "35.230000",
"Stock": "0"
},
{
"Productcode": "0160-040",
"Description": "AXIS MPEG-4 +ACC Decoder 50-user license onto 50 separate computers. For all Axis products that supports MPEG-4. (207 207W 207MW 212PTZ 216FD 223M).",
"ThumbPic": "NoImage.png",
"RRP": "50.880000",
"Stock": "0"
},
{
"Productcode": "10403E",
"Description": "Hotsync palm computer cradle/docking station",
"ThumbPic": "NoImage.png",
"RRP": "0.000000",
"Stock": "2"
},
{
"Productcode": "0903-000193",
"Description": "ICROCOMPUTER;32FX200 24BIT QFP 132P",
"ThumbPic": "NoImage.png",
"RRP": "37.790000",
"Stock": "0"
}
]
}
}
I have managed to do it when it’s only one result with this code
if(data !== '')
{
xmlData = data.childNodes[0].childNodes[0].data;
objData = app.XML2OBJ(xmlData);
var item = objData.MasterProduct.Description;
//alert(item);
}
But can’t get it to work with multiple sets of results.
As people have been discussing in the comments above, you can simply use normal vanilla JavaScript to access the data.
Will give you
0903-000192. Objects that contain square brackets[]are arrays. For them you can use the array notation ofarr[0]. For objects, you can use their key names.MasterProductsis your root object, so you can use it as your entry point.Now, to get the first element of the list of products you could use array notation.
To get at one of the properties of that element, you could then again use the dot notation –