Forum Moderators: open

Message Too Old, No Replies

JS object to JSON ignores objects arrays?

         

dprevite

5:00 pm on Feb 8, 2007 (gmt 0)

10+ Year Member



I have an object similar to this:

Item.includes['left'] // An array of IDs of included items
Item.includes['right'] // Same
Item.price // Item price

Then I convert it using:


Item.toJSONString() // From http://www.json.org/json.js

All I get back is:

{"price":123,"includes":[]}

I don't get any of the values from the arrays inside of
Item.includes
, does anyone know how I can convert the object to JSON and get all of the arrays as well?

Fotiman

5:40 pm on Feb 8, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Item.includes['left'] // An array of IDs of included items
Item.includes['right'] // Same

Without seeing your entire code, I'd say this piece doesn't do what you think it does. You have an object "Item" which has a property "includes". "includes" is an object which has properties "left" and "right". "left" and "right" both contain arrays of ids. So that might look like this:


var Item = {
"includes" : {
"left" : [1,2,3],
"right" : [4,5,6]
},
"price" : "$1.00"
};

You get back this:

{"price":123,"includes":[]}

That indicates that "includes" contains an array, NOT another object like your syntax above suggests. If that's true, then your syntax of:
Item.includes['left']
and
Item.includes['right']
is incorrect (what index does the string 'left' and 'right' correspond to if this is an array?)

dprevite

5:51 pm on Feb 8, 2007 (gmt 0)

10+ Year Member



Ah, that seems to have been it.

I had Item.includes = Array() and I was adding things into an array. I changed it to Object(), and now it works fine.

Thank you