I have a variable with the following in my code:

{
"Rows":
    [
        {
            "New":1,
            "CachedNumberType":0,
            "Date":1327479615921,
            "Type":2,
            "Number":"123456",
            "Duration":1
        }
    ]
}

I think it's JSON, how do I parse it? (E.g., with json2.js?) Or how do I use it in my JavaScript?

link|improve this question

feedback

3 Answers

up vote 1 down vote accepted

You've said that when you try JSON.parse on the variable containing the "JSON" that it says it can't parse it. Could it be that it's already deserialized? Or maybe it was never JSON at all? For instance, what you quoted, in JavaScript source, is an object literal containing an array literal containing another object literal; no JSON in sight.

If you do console.log(x.Rows[0].Date);, where x is the variable you were trying to pass to JSON.parse, do you see the date value?

A lot of people confuse JSON and JavaScript literal syntax, because JSON is a textual format derived from JavaScript literal syntax. I suspect that's what's happening here.

link|improve this answer
thanks Mate :D:D – Coder_sLaY Jan 25 at 11:03
feedback
var jsonObj = JSON.parse(jsonString);
link|improve this answer
I have an Object which contains the string... How will i convert that to JSON string? – Coder_sLaY Jan 25 at 9:56
@Coder_sLaY: Just pass the object that holds the json string as the argument to JSON.parse – xbonez Jan 25 at 10:00
it is saying unable to parse JSON string. :( – Coder_sLaY Jan 25 at 10:04
@Coder_sLaY: If you have that string assigned to a variable, please make sure all the quotes have been escaped or it will end the json string prematurely. – xbonez Jan 25 at 10:28
@Coder_sLaY: Then are you sure it's a JSON string? Could it be that it's already deserialized? Or may be it was never JSON at all? (For instance, what you quoted, in JavaScript source, is an object literal containing an array literal containing another object literal; no JSON in sight.) What happens if you do console.log(x.Rows[0].Date);, where x is the variable you were trying to pass to JSON.parse. (If you don't know console.log, use alert instead.) – T.J. Crowder Jan 25 at 10:29
show 2 more comments
feedback
var obj = jQuery.parseJSON(json);

For more details http://api.jquery.com/jQuery.parseJSON/

link|improve this answer
1  
The question has nothing to do with jQuery. – T.J. Crowder Jan 25 at 9:58
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.