0

I have long json answer, here it is:

{"head":{},"def":[{"text":"hello","pos":"noun","ts":"?he?l??","tr":[{"text":"привет","pos":"noun","syn":[{"text":"приветствие","pos":"noun","gen":"ср"}],"mean":[{"text":"hi"},{"text":"welcome"}],"ex":[{"text":"big hello","tr":[{"text":"большой привет"}]}]}]},{"text":"hello","pos":"verb","ts":"?he?l??","tr":[{"text":"поздороваться","pos":"verb","asp":"сов","mean":[{"text":"greet"}]}]}]}

As you can see, there are some inner massives and I can't get them out! I could only get "def" key, but I get get, for example, "tr" or "mean".

I found code here, that can be useful for me:

public static void doJson(String link) throws ParseException {
    JSONParser parser = new JSONParser();
    JSONObject jj = (JSONObject) parser.parse(link);
    String died = (String) ((JSONObject) ((JSONObject) jj.get("def")).get("tr")).get("text");
    System.out.println(died);
}

But it gives me back an error:

Exception in thread "main" java.lang.ClassCastException: org.json.simple.JSONArray cannot be cast to org.json.simple.JSONObject
at StringOfTrans.doJson(StringOfTrans.java:50)
at StringOfTrans.main(StringOfTrans.java:19) 

So, I thinkm that's right way, but I need to fix it. How can I do it? I want to work with json-simple and that's my first experience with JSON. So, I've read already how to get objects from JSON if it contains only one array, but I can't find good examples with json-simple for parse inner arrays. Thank you!

Vollmilchbb
  • 481
  • 6
  • 20
Lame Lane
  • 11
  • 1
  • 8
  • `jj.get("def")` gives you an object of type `JSONArray`, so don't cast it to `JSONObject`, cast it to `JSONArray` and work with it as with a `JSONArray`. Particularly, you won't be able to access anything in it by name (`...get("tr")`), you should first pick a `JSONObject` from that array. – starikoff Mar 25 '17 at 10:51
  • So, can you show me example? First I get JSONArray jj1= jj.get("def") and the I should create JSONObject = jj1.get("tr"), and the create String or Array for "text" from "tr" or "mean" array? – Lame Lane Mar 25 '17 at 13:31

1 Answers1

0

Judging by the structure of JSON you can get the value of the "text" like this:

JSONParser parser = new JSONParser();
JSONObject jj = (JSONObject) parser.parse(link);
JSONArray  def = (JSONArray) jj.get("def");
JSONObject tr  = (JSONObject) def.get(0);
String text = (String) tr.get("text"); //value is hello

You should see that "def" actually is a JSONArray object. You should change the code to get different nodes with "text" values as you need, this is just an example to show you that you will recieve JSONArray objects along the way. If for some reason you would want to get the mean array. You would just repeat the stept in the code above as this would apply to any other element in the json file.

JSONArray trArr = (JSONArray) tr.get("tr");
JSONObject meanNode = (JSONObject) trArr.get(0);
JSONArray meanArr = (JSONArray) meanNode.get("mean");

The only difficulty here is to identify your JSON structure to understand whether you will have an array or an object.

Vollmilchbb
  • 481
  • 6
  • 20