-1

UDPATE: SO WHATS THE BEST WAY TO ILLITERATE THROUGH A MULTI DIMENSIONAL ARRAY. SO CHECKING FOR OBJECTS INSIDE COMPLEX ARRAYS AND GRABBING THEIR VALUES.


{ "error":{
      "data": {
             "id":"3",
             "name":"404",
             "content" : "value"
       },
      "response":"Invalid path.",
      "code": 1
   }
}

if(json.error.data  !== undefined) {
      alert('value: ' + json.error.data);
}

I'm having an issue here; I can't seem to correctly resolved. When I try to check an array for a value it returns the error "Uncaught TypeError: Cannot read property 'data' of undefined" if the array data doesn't exist or content isn't present. I know there is a way to use indexOf() to test if the value is present, but this doesn't help me. I need to test for that value and then capture it. Remember, this is a multidimensional array so just using indexOf won't necessarily locate the complexity of the array and how to locate it.

Ok, so put simply. I need to say, does the array data and the parameter content exist in the error array shown above. If it does, then capture it's value and do something.

I also read about jQuery.inArray(); I don't know how to utilize this for complex multidimensional arrays.

Is the only successful way to do this using a loop for( x in z) {}

9
  • If you're seeing "Cannot read property 'data' of undefined" when trying to access json.error.data, then the problem is not that data is undefined, but that error is undefined. Commented Feb 29, 2012 at 3:44
  • This is obviously not your actual code. Is the string a way of saying you're getting JSON data from the server? Commented Feb 29, 2012 at 3:51
  • The json data is converted into an object and stuck into an array. When I try to access that array to test if another array with a value exists I get an error. Commented Feb 29, 2012 at 3:53
  • So show your actual code. The code you've posted has an obvious problem that json is a string not an object, and people are writing answers about that. We can make arbitrary guesses about what might be wrong with the code you don't show, but that just wastes everybody's time. Commented Feb 29, 2012 at 3:58
  • @nnnnnn I can't show my actual code because it's an array object located in a backbone.js model that pulls json my my restful api. So the next best thing was simply showing you my json so you could understand its structure. Commented Feb 29, 2012 at 4:20

5 Answers 5

1

To test for undefined, use typeof and the string 'undefined'.

Also, check for the existence of the .error parent property of .data after parsing the string into JSON. It isn't clear from your code whether that has actually been done.

var obj = JSON.parse(json);

if (typeof obj.error !== 'undefined') {
  if(typeof obj.error.data  !== 'undefined') {
        alert('value: ' + obj.error.data);
  }
}
4
  • The console is outputing, Uncaught TypeError: Cannot read property 'data' of undefined Commented Feb 29, 2012 at 3:48
  • "You cannot compare undefined with equality operators" - sure you can: if (obj.error === undefined) (assuming you've ensured that undefined has not been given a defined value), or if (obj.error === void 0). Commented Feb 29, 2012 at 4:00
  • Thanks for actually taking the time to understand this issue. Michael is there anyway to avoid the process of all these if statements. Isn't there a jQuery function that will check through a multi-dimensional array for a object and then obtain it's value. Commented Feb 29, 2012 at 4:01
  • @kr1zmo Not sure about a jQuery function, but you can shorten to if (obj.error && obj.error.data), however that won't test specifically for undefined data. It could be null or false, and the condition would still pass. Commented Feb 29, 2012 at 4:18
1

https://stackoverflow.com/a/2313663/528858

try{
   var json = JSON.parse(this.responseText);
}catch(e)
   alert('invalid json');
}
0

I think you got your object wrong.

json = '{...

You are starting with a single quote. Variable json is now a string, not a JavaScript object.

3
  • I just showed you the json so you could understand what I am doing. In my code the json data is converted into an object. But when I try to test for a value I get that error. Commented Feb 29, 2012 at 3:45
  • It's not even a string. It's a SyntaxError. Commented Feb 29, 2012 at 3:52
  • Yeah, that's why I removed the whitespace, to illustrate my point. But foremost, it is a syntax error. Commented Feb 29, 2012 at 3:53
0

declare your object as this

json = 
{ 
    "error":{
        "data": {
            "id":"3",
            "name":"404",
            "content" : "value"
        },
        "response":"Invalid path.",
        "code": 1
    }
};
-1

I think its as simple as the fact that you've surrounded your object with quotes, which makes its a string instead of an object. Remove those and it should work fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.