-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) {}

kr1zmo
  • 837
  • 3
  • 13
  • 30
  • 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. – CodeThug Feb 29 '12 at 03:44
  • This is obviously not your actual code. Is the string a way of saying you're getting JSON data from the server? –  Feb 29 '12 at 03: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. – kr1zmo Feb 29 '12 at 03: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. – nnnnnn Feb 29 '12 at 03: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. – kr1zmo Feb 29 '12 at 04:20
  • OK, well I can see where you were coming from. If you find yourself posting a similar question in future I suggest you show the object as if it was created from an object literal, that is, as above except _unquoted_. That way people won't assume the quotes are the problem... – nnnnnn Feb 29 '12 at 04:25
  • @nnnnnn, I still can't believe without a loop javascript doesn't offer a better way to search multidimensional objects (Arrays) for an array or object located within it and grab its value. Do you know if there is any way to use jQuery.inArray(); and callback its value. – kr1zmo Feb 29 '12 at 04:31
  • Sorry, I don't understand what you mean by "searching" when you seem to already know the names of the properties that you want the value from. Checking for `undefined` is no different to the equivalent `!= null` test that you'd have to do in Java or C#. – nnnnnn Feb 29 '12 at 04:56
  • possible duplicate of [Get query string values in JavaScript](http://stackoverflow.com/questions/901115/get-query-string-values-in-javascript) – Caleb Mar 25 '12 at 04:05

5 Answers5

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);
  }
}
Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390
  • The console is outputing, Uncaught TypeError: Cannot read property 'data' of undefined – kr1zmo Feb 29 '12 at 03: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)`. – nnnnnn Feb 29 '12 at 04: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. – kr1zmo Feb 29 '12 at 04: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. – Michael Berkowski Feb 29 '12 at 04:18
1

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

try{
   var json = JSON.parse(this.responseText);
}catch(e)
   alert('invalid json');
}
Community
  • 1
  • 1
Zhasulan Berdibekov
  • 1,077
  • 3
  • 19
  • 39
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.

Rémi Breton
  • 4,209
  • 2
  • 22
  • 34
  • 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. – kr1zmo Feb 29 '12 at 03:45
  • It's not even a string. It's a SyntaxError. –  Feb 29 '12 at 03:52
  • Yeah, that's why I removed the whitespace, to illustrate my point. But foremost, it *is* a syntax error. – Rémi Breton Feb 29 '12 at 03:53
0

declare your object as this

json = 
{ 
    "error":{
        "data": {
            "id":"3",
            "name":"404",
            "content" : "value"
        },
        "response":"Invalid path.",
        "code": 1
    }
};
ianace
  • 1,646
  • 2
  • 17
  • 31
-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.

TheDelChop
  • 7,938
  • 4
  • 48
  • 70