I always get confused on how to properly format json. I think its because I need to walk away and take a breather.
I have a have a ecommerce system and I have a script that I want to display category information via json. Will displaying it this way:
{
"cat_name": "All Items",
"cat_desc": "<div><img src=\"item.gif\" /></div>",
"cat_desc2": "",
"cat_id": "756",
"cat_father_id": "0",
"cat_image": "",
"per_ship": "0.00",
"item_ship": "0.00",
"item_int_ship": 0,
"per_int_ship": "0.00",
"noProducts": "45",
"disp_order": "3",
"cat_img_folder": ""
}
Would I be able to reference these items after being initlized to an object like so:
obj.cat_name
obj.cat_id
That’s properly formated, if your service returns that information you could do the following
where key will be the property and val the value when you access it (in the fashion data[key] === value, or, data.key === value).
So, yes, if
datahas the key that you’re looking for (in your casecat_name cat_id, you can get them.One thing to have in mind is that Numbers doesn’t need to be between
"", if you want them to be numbers when you access them via object propertiesAlso, if you ever need to know if you’re formatting your json correctly you could take a look at this link