0

I have received a JSON data like this:

'{"result":[[["test","test"],["test","test"]],[],[],[]]}'

OR

'{"result":[
            [
             ["test","test"],
             ["test","test"]
            ],
            [],
            [],
            []
           ]
 }'

But when I try to JSON.parse(data);, it is converted to object like this:

{[[["test","test"],["test","test"]],[],[],[]]:}

Is there anyway to fix it?

ADDITIONAL: i have traced what happen before,during,after JSON do, and the problem seems to be the parse itself, sometime it work, some time doesn't

var object:Object = {test:[[["Test","test1"],["test2"]],["test3"],[],[]]}
var stringed:String = JSON.stringify(object);
trace(stringed)//{"test":[[["Test","test1"],["test2"]],["test3"],[],[]]}
var backed:Object = JSON.parse(stringed);
for each(var thigng:String in backed){
    trace(thigng, "=", backed[thigng])//Test,test1,test2,test3,, = undefined
}

var object:Object = {"test":"test3"}
var stringed:String = JSON.stringify(object);
trace(stringed)//{test:"test3"}
var backed:Object = JSON.parse(stringed);
for each(var thigng:String in backed){
    trace(thigng, "=", backed[thigng])//test3 = undefined
}

1 Answer 1

1

A "for each...in" loop will only give you the value not the key.

What you need is the for in loop.

As you can see from the example below where you went wrong

var object:Object = {"this_is_the_key":"VALUE"}
var stringed:String = JSON.stringify(object);
var backed:Object = JSON.parse(stringed);
for each(var thigng:String in backed){
    trace('KEY:', thigng, '  VALUE:' ,backed[thigng]) // KEY: VALUE   VALUE: undefined
}
trace('------')
for(thigng in backed){
    trace('KEY:', thigng, '  VALUE:' ,backed[thigng]) //KEY: this_is_the_key   VALUE: VALUE
}

Also this is not a valid JSON string

'{"result":[[["test","test"],["test","test"]],[],[],[]]}'
Sign up to request clarification or add additional context in comments.

3 Comments

Out of curiosity, why is it not valid? Just tested it with JSONLint and it passes. Otherwise, this appears to be the correct answer and what I was going to suggest.
@JoshJanusch Actually I just ran it through a validator and it passed so guess I was wrong on that part. But what I was referring to was the fact that the second level of the nested array didn't have name value pairs.
Doesn't have to since it is an array, not an object.

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.