JSONObject not found
I'm having some trouble with JSON and Java, I have a working function to export a data strucutre into JSON and save it in a file. But when I try to do the reverse and import the data strucutre back into java, I get an error, the output of which is:
java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at ..... Caused by: java.lang.RuntimeException: JSONObject["nodeID"] not found. at processing.data.JSONObject.get(JSONObject.java:549) at processing.data.JSONObject.getString(JSONObject.java:568) at Graph.loadJSON(Graph.java:934) ... 21 more
From that error I take that the String 'nodeID' can't be found within the node, but it is in the JSON output, which looks like this:
{"network": [ {"node": { "nodeID": "1", "radius": 40 "atom": { "id": "a-2675-EH", "activeTime": 0, "type": "Motor", "linkFrom": [{ "atomID": "a-2312-TL", "delay": 0.20000000298023224 }], "linkTo": [{ "atomID": "a-3289-Ql", "delay": 0.20000000298023224 }] }, .....
The JSON code is an array of nodes contained within network, then each node object contains a String nodeID, and 3 Float variables, then an Object called atom. This atom then contains two Strings, a float, and 2 arrays called linkTo and linkFrom, which contain objects to represent a link (atomID and delay, string and float respectfully)
And the Java code to load each node from the network looks like this:
JSONObject network = loadJSONObject(selection.getAbsolutePath()); JSONArray nodes = network.getJSONArray("network"); for (int i=0; i<nodes.size(); i++) { //Load node JSONObject node = nodes.getJSONObject(i); String nodeID = node.getString("nodeID"); <-- Error occurs here Float x = node.getFloat("x"); Float y = node.getFloat("y"); Float radius = node.getFloat("radius"); JSONObject atom = node.getJSONObject("atom"); .....
I'm sure I'm just missing something simple, but I've spent nearly a week trying to tweak this and getting nowhere!
I know the node is loading properly, because a println(node) statement inside the for loop prints out the node exactly as it should, it's just trying to fetch the nodeID that seems to not work. Even if I change the order and try to fetch the x, or y float that causes the error too.
Thanks.