I posted here earlier and was flagged for my post being too similar to another previously answered. I don't believe this is, so have tried to reword it.
I have a JSON file where value is either a long, or can contain an object and a array:
{
"declaration-list" : [
{
"declared-variable" : "x301",
"value" : {
"operator" : "set",
"arguments" : [
0,
1
]
}
},
{
"declared-variable" : "x112",
"value" : -1
},
]
}
I have written the following code in java to parse the file and have managed to extract declared-variable from the array (Note: I'm using org.json.simple):
public static void getInput() throws IOException, ParseException {
JSONParser parser = new JSONParser();
File file = new File("myfile");
Object object = parser.parse(new FileReader(file));
JSONObject jsonObject = (JSONObject) object;
JSONArray jasonArray = (JSONArray) jsonObject.get("declaration-list");
for (Object JSI : jasonArray) {
if (JSI instanceof JSONObject) {
JSONObject declared_variable = (JSONObject) JSI;
String decVar = (String) declared_variable.get("declared-variable");
System.out.println(decVar);
JSONObject value = (JSONObject) JSI;
String operator = (String) value.get("operator");
System.out.println(operator);
}
}
}
How do I get a long from a JSONArray and deal with the case where value contains another JSONObject and JSONArray?
I hope this post is more clear, thanks.