0

I'm now writing a test html program to check well-formed JSON data send by web socket, and i'm stuck here:

parsing JSON String while I do not know the exactly keys of it.

jQuery.parseJSON() can return the resulting JavaScript object, but I need to know each key to access the data.

I may not make my question clearified, sorry 'bout that.What I want is to make data more readable instead of a long JSON String.

3
  • This is a chicken and egg problem. What do you want us to tell you? If you need to know the keys, then you need to know the keys; otherwise, why do you need to know the keys? If you just want to know whether the JSON is strictly well-formed, well parseJSON may throw an exception if it's not (no word yet on what "may" means here). Commented Jan 15, 2013 at 3:26
  • I may not make my question clearified, sorry 'bout that.What I want is to make data more readable instead of a long JSON String. Commented Jan 15, 2013 at 3:29
  • Yeah that seems like a totally different question... Commented Jan 15, 2013 at 4:18

2 Answers 2

4
for(i in json) {
 console.log(i);
 console.log(json[i]);
}

Is this construct of any use?

As others have pointed out, you could use the jQuery .each() to iterate over it as well.

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

2 Comments

What if it's a Nested JSON String?
You can probably test what the first character is "{" or otherwise and make a first approximation guess about whether to do another loop?
2

You can also use the jQuery.each() to iterate throw all the keys in the json object.

Ex:

$.each({'key':'value'}, function(i, v) { 
    console.log(i, v)
});

Comments

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.