从JSON提取字段的最简单方法 [英] Easiest way to extract fields from JSON

查看:214
本文介绍了从JSON提取字段的最简单方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:我应该立即提到这一点:我首先考虑了Java/JSON映射框架,但是我的经理不希望我向项目添加更多依赖项,因此作为一种选择. JSON-Java jar已经在我们的类路径中,因此我可以使用它,但仍然看不到森林树木如何使用.

Update: I should have mentioned this right off the bat: I first considered a Java/JSON mapping framework, but my manager does not want me adding any more dependencies to the project, so that is out as an option. The JSON-Java jar is already on our classpath, so I could use that, but still not seeing the forest through the trees on how it could be used.

我的Java程序正以以下形式传递给JSON(尽管值将一直更改):

My Java program is being handed JSON of the following form (althought the values will change all the time):

{"order":{"booze":"1","handled":"0","credits":"0.6",
    "execute":0,"available":["299258"],"approved":[],
    "blizzard":"143030","reviewable":["930932","283982","782821"],
    "units":"6","pending":["298233","329449"],"hobbit":"blasphemy"}}

我正在寻找从该JSON字符串中挑选特定值并将其聚合为List<Long>的最简单,有效,肯定的方法.

I'm looking for the easiest, efficient, surefire way of cherry-picking specific values out of this JSON string and aggregating them into a List<Long>.

具体来说,我希望提取并汇总所有" id ",即您在availableapprovedreviewablepending字段.这些字段中的每一个都是一个包含0+个"id"的数组.因此,在上面的示例中,我们看到了ID的以下细分:

Specifically, I'm looking to extract-and-aggregate all of the "ids", that is, all the numeric values that you see for the available, approved, reviewable and pending fields. Each of these fields is an array of 0+ "ids". So, in the example above, we see the following breakdown of ids:

  • available:具有1个ID(299258)
  • approved:具有0个ID
  • reviewable:具有3个ID(930932、283982、782821)
  • pending:有2个ID(298233,329449)
  • available: has 1 id (299258)
  • approved: has 0 ids
  • reviewable: has 3 ids (930932, 283982, 782821)
  • pending: has 2 ids (298233, 329449)

我需要一些Java代码来运行并产生带有所有6个提取的id的List<Long>,且不按特定顺序.这些ID只需进入列表即可.

I need some Java code to run and produce a List<Long> with all 6 of these extracted ids, in no particular order. The ids just need to make it into the list.

这感觉像一个令人难以置信的复杂正则表达式,我什至不知道从哪里开始.任何帮助都将不胜感激.预先感谢.

This feels like an incredibly complex, convoluded regex, and I'm not even sure where too begin. Any help at all is enormously appreciated. Thanks in advance.

推荐答案

IMO使用json库的最简单方法,例如 gson jackson

The easiest way IMO is use a json library such as gson, jackson, json.org, etc, parse de JSON into an object and create a new List<Long> with the values of the properties you need.

带有gson的伪代码:

Pseudocode with gson:

class Order {
   long[] available;
   long[] approved;
   ...
}

Order order = gson.fromJson("{ your json goes here }", Order.class);

List<Long> result = new ArrayList<Long>();
result.add(order.getAvailable());
result.add(order.getApproved());
...

带有json.org/java的伪代码:

Pseudocode with json.org/java:

JSONObject myobject = new JSONObject("{ your json goes here"});
JSONObject order = myobject.getJSONObject("order");

List<Long> result = new ArrayList<Long>();
for (int i=0; i<order.getJSONArray("approved").length(); i++) {
    Long value = order.getJSONArray("approved").getLong(i);
    result.add(value);
}
...

这篇关于从JSON提取字段的最简单方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆