Say I have an array like this: {Id, Value}
This is not an array. This is an object. You can use multiple copies in an array:
[ {"foo": "bar"}, {"foo": "baz"}, {"foo": "boom"} ]
This is a valid JSON string for an array containing objects - in this case, objects with one property, foo , each of which has its own value, but the objects should not have the same properties, and they can have several properties. For example:
[ {}, ["zero", "one", "two", "three"], "I'm just a string", { "f0": "foo zero", "f1": "foo one", "f2": "foo two", "all": ["foo zero", "foo one", "foo two"] }, 42 ]
This is a valid JSON string for an array with five entries:
- An object without properties (for example, an "empty" object).
- An array with four elements.
- Line.
- An object with four properties:
f0 , f1 , f2 and all . f0 , f1 and f2 all have string values; all has an array of strings as value. - The answer to Life, the Universe and Everything.
You can turn an object or an array into a valid JSON string (stringify) and cancel the process (parsing) on the client side using any of several libraries. Crockford (a JSON investor) has several githubs on his page , primarily json2.js, although json2.js relies on eval for parsing; since this is not entirely ideal, you can use json_parse.js (a recursive descent parser that does not use eval ) or json_parse_state.js (a state machine that does not use eval ).
Tj crowder
source share