简体   繁体   English

使用json.simple在单行中解析JSONObject内的JSONObject

[英]Parsing a JSONObject inside a JSONObject in a single line with json.simple

Lets say I want to parse a JSONObject with JSONObjects inside of it, which I parse from a string. 可以说我想解析一个JSONObject,其中包含JSONObjects,我从一个字符串中进行解析。 I want to do this in a single line, like I've done with other libraries, but I'm not sure how. 我想单行执行此操作,就像我对其他库所做的一样,但是我不确定如何执行。

   JSONParser parser = new JSONParser();
   Object obj = parser.parse(test);
   JSONObject first = (JSONObject) obj;
   JSONObject second = (JSONObject) first.get("feed");
   JSONArray third = (JSONArray) second.get("entry");
   JSONObject fourth = (JSONObject) third.get(0);
   JSONObject fifth = (JSONObject) fourth.get("test");

Is there a way for me to get all these JSONObjects in a single line? 有没有办法让我在一行中获取所有这些JSONObject? With another library I'd just do first.getJSONObject("feed").getJSONArray("entry").getJSONObject(0) etc, but I'm not sure how to do it properly with this library. 使用另一个库,我只需要先执行。getJSONObject(“ feed”)。getJSONArray(“ entry”)。getJSONObject(0)等,但是我不确定如何使用此库正确执行。

Thanks. 谢谢。

我建议使用Google的Gson( https://github.com/google/gson )库。

You can cast within a single line like this: 您可以像这样在一行中进行投射:

(JSONObject) ((JSONObject) YOURJSONOBJECT.get("YOUR_KEY")).get("ANOTHER_KEY");

This can get messy really quick depending on how many layers deep you have to go though 这可能很快就会变得混乱,具体取决于您需要走多少层

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

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