jsonUtils usage

You are here

3 posts / 0 new
Last post
jsonUtils usage

Hi all,

I'm creating a repository tier webscript that processes content posted as json. In the script, I got the root 'json' object ok, but when I try to convert it to a native js object using jsonUtils.toObject(json), I can't figure out what's going on...

example posted json:
{"a": [{"b": "c"}, {"d": "e"}]}

when i do:

var test = jsonUtils.toObject(json);

Using the alfresco javascript debugger and the Expression window, here's what I see:

json                      {"a": [{"b": "c"}, {"d": "e"}]}
test TypeError: Cannot find default value for object.
test.a [{"b": "c"}, {"d": "e"}]
test.a.size function size() {/* int size() */}
test.a.get function get() {/* java.lang.Object get(int) */}
test.a[0] Java class "org.json.simple.JSONArray" has no public instance field or method named "0".
test.a.get(0) null
test.a.getJSONObject(0) TypeError: Cannot find function getJSONObject.

This is confusing enough. From the documentation it seems that jsonUtils.toObject should return a native javascript object, except array values were not converted, and I can't even use functions from JSONArray java object? I'm struggling to iterate over the array that's the value of "a".

(I also tried to import and use the json2.js file at https://github.com/douglascrockford/JSON-js instead, but it failed to import and choked when trying to add the toJSON function to Date.prototype)

Any help is appreciated!


I have aways struggled myself with this & generally do jsonVar = eval( "(" + jsonString + ")" 0;

Good luck

Bob Johnson

P.S. If this was useful, please mark it as such - it might help someone else to.

Thanks, I thought about doing

Thanks, I thought about doing that, but eval can't safely be used in production... What I ended up doing is importing the json2.js, but comment out the part that adds toJSON functions to Date.prototype, etc. That imported successfully and I can do JSON.parse. (That means I can't do stringify, but I don't need that in this case)

forums index