-1

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!

adnan
  • 1,385
  • 2
  • 17
  • 31
  • Felix put an answer for one of those, let me find it. – Benjamin Gruenbaum Jan 20 '14 at 18:48
  • 2
    `Now if I'm correct a square bracket means that it's an object` that is incorrect. square bracket is an array. However, your `jsonresp.resource.street` should have given you the value. http://jsfiddle.net/6fYag/ – Kevin B Jan 20 '14 at 18:48
  • `jsonresp.resource.street` should work for that object (note I had to change `number` to something defined): http://jsfiddle.net/5Sbw3/ – Jason P Jan 20 '14 at 18:50
  • 1
    If `jsonresp.resource.street` does't give you the value you expect, then the structure of the object is not what you posted here. `console.log` each nested property (step by step) and inspect the value to find out how you have to access it. – Felix Kling Jan 20 '14 at 18:52
  • What is `response`? What does `console.log(jsonresp);` show? – gen_Eric Jan 20 '14 at 18:53
  • 1
    if `jsonresp.resource.street` returns undefined then your `jsonresp` variable doesn't look like you think it does – J Max Jan 20 '14 at 18:53

2 Answers2

1

This is basic javascript: a {...} (curly braces) defines an object literal and a [...] (square braces) defines an array. If you have

var myObj = {
   someProperty: 'property value'
};

myObj.someProperty // evaluates to 'property value'

or

myObj['someProperty'] // evaluates to 'property value'

As is mentioned in your the comments to your question, if the jsonresp.resource.street code returns undefined, then your json structure is not what you think it is.

hvgotcodes
  • 118,147
  • 33
  • 203
  • 236
0

Try jsonresp.resource.street again after putting number in quotes.

jsonresp = 
{
  "success": true,
  "resource": {
    "street": "the address",
    "house_number": "number",
    "postcode": "postcode",
    "town": "city",
  }
}
KingKongFrog
  • 13,946
  • 21
  • 75
  • 124