I'm surprised json.length
is returning anything; that JSON string is invalid. The outermost curly braces ({}
) denote an object, which must contain keys and values, but it just contains a value (the array, with no key).
If you remove the curly braces, it should work correctly. Did you put them there, perhaps because you'd seen it done? If so, you want parentheses (()
), not curly braces.
Note that using eval
on JSON strings is not safe, you want to use a JSON decoder that doesn't use eval
(like json2.js, which uses eval
, but only after very carefully ensuring it's safe to do so, modifying it if necessary), for optimum safety. The parentheses help, but they're not at all a complete solution.