2

Here is my Json data:

[{"ok" : false,
  "details" : [{"n" : "Email",
                "v" : "The email address you entered is not correct."},
               {"n" : "NameFull",
                "v" : "Please enter your full name."}]
 }]

I found read sigle-level Json data is super-easy but for multi-level data like above I couldn't find an answer by search the internet. I just want to read above values. Like reading value from "n" and "v" under the "details".

4
  • Can you change this into source code, please? Commented Jan 19, 2009 at 8:49
  • The Json data already in source code mode, but maybe StackOverflow can't highlight Json text. Commented Jan 19, 2009 at 8:54
  • and it should looks pretty too :) Commented Jan 19, 2009 at 9:01
  • That's really cool! Thank you! ;o) Commented Jan 19, 2009 at 9:01

2 Answers 2

6

The data is inside an array

Note the square brackets around your data. You will have to index into the array in the above set of data.

This should let you access the email value:

var json = '[{"ok":false,"details":[{"n":"Email","v":"The email address you entered is not correct."},{"n":"NameFull","v":"Please enter your full name."}]}]';

var k = eval( json );

alert( k[0].details[0].v );

And this is the alert text that appeared:

The email address you entered is not correct.

Maybe your JSON source put it inside an array because they want to return multiple elements?

And actually, you don't need jQuery for this. JSON stands for JavaScript Object Notation. That basically means that it is a Javascript block of code that can be evaluated as-is directly in JavaScript to get the serialized data, which, in your case happens to be an array.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! It works! I'm a super newbie, did you mean that I Json data I provided is not a standard Json? Thanks again!!!
It is a standard JSON, it's just inside an array.
1

Sorry, maybe I don't understand your question. This doesn't work for you?

var data = eval('(' + strJson + ')');

3 Comments

Sorry for making misunderstood, I want to read some specific keys and values from that Json text above by using jQuery.
silent200: what you need the is jQuery.getJSON(), it will fetch a json from somewhere and do the eval for you. You don't need jquery to handle json, the support is "built into" javascript.
silent200: My turn to apologize. I thought you know that eval() finction converts your JSON-encoded string into normal JavaScript object with properties and indexer. I certainly had to explain this clearly :)

Your Answer

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