简体   繁体   English

json-simple Java:错误在哪里以及如何解决?

[英]json-simple Java: where's error and how to solve?

I have long json answer, here it is: 我有很长的json答案,在这里是:

{"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". 我只能获取“ def”键,但可以获取例如“ tr”或“ 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. 我想使用json-simple,这是我第一次使用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. 因此,我已经读过如何从JSON获取对象(如果它仅包含一个数组),但是我无法找到带有json-simple的用于解析内部数组的良好示例。 Thank you! 谢谢!

Judging by the structure of JSON you can get the value of the "text" like this: 通过JSON的结构判断,您可以像这样获得“文本”的值:

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. 您应该看到“ def”实际上是一个JSONArray对象。 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. 您应该更改代码以根据需要获取具有"text"值的不同节点,这只是一个示例,向您展示了您将在此过程中接收JSONArray对象。 If for some reason you would want to get the mean array. 如果由于某种原因,您想要获取mean数组。 You would just repeat the stept in the code above as this would apply to any other element in the json file. 您只需在上面的代码中重复stept,因为这将适用于json文件中的任何其他元素。

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. 唯一的困难是识别JSON结构,以了解您将拥有数组还是对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM