It feels like I'm posting a duplicate but I just can't find a satisfactory answer. I've got a JSON object that I've parsed and it looks like:
var jsonresp = $.parseJSON(response);
jsonresp =
{
"success": true,
"resource": {
"street": "the address",
"house_number": number,
"postcode": "postcode",
"town": "city",
}
}
Now if I'm correct a square bracket means that it's an object. But what does a curly bracket represent?
Let's say I want to get the value of street, why does jsonresp.resource.street
return undefined? In other words what are the contents of the resource between the curly brackets and how should I select them?
EDIT: thanks for all the reactions. It seems like something else is going wrong.
So this is the code I use:
$.post(ajaxurl, postretrieve, function(response) {
var jsonresp = $.parseJSON(response);
console.log(jsonresp);
var jsonresp2 = {"success":true,"resource":{"street":"streetname","house_number":20,"postcode":"postcode","town":"city"}};
console.log(jsonresp2.resource.street);
}
I exactly copy paste what is in jsonresp and make that a second variable jsonresp2. jsonresp2.resource.street
returns the correct value but jsonresp.resource.street
gives an error: Uncaught TypeError: Cannot read property 'street' of undefined
What am I doing wrong here?
EDIT2: I was json_encoding my output file in php that was already json_encoded, thanks for the help!