I am trying to parse a son file, and I don\'t know what I\'m doing wrong (of course, I don\'t really know what I\'m doing right, either).
file.json
When the JSonParser
parses the file, it's returning it as a JSONArray
, to solve it try to use this:
JSONObject obj = (JSONObject)obj;
JSONObject elem = (JSONObject)obj.get("0");
String unit = (String) elem.get("elemTwo");
System.out.println(unit);
you are getting JSONArray not JsonObject
JSONParser parser = new JSONParser();
Object obj = parser.parse(new FileReader("/path/to/file.json"));
JSONArray json = (JSONArray) obj;
Than loop this and get jsonobject
You should cast your obj
toJsonArray
instead of JsonObject
, because your json file has []
at the root.