如何从API中提取JSON字段

发布于 2025-01-19 17:35:19 字数 1250 浏览 0 评论 0 原文

当我使用clientbuilder get():

final Response response = ClientBuilder.newClient().target("url").queryParam("CustomerQuery", jsonarr).request(MediaType.APPLICATION_JSON).get();
String actual = response.readEntity(String.class);
System.out.println(actual);

结果:

{"_id":{"timestamp":1649320244,"date":"2022-04-07T08:30:44.000+00:00"},"ScheduleTime":"2022-04-07T09:50:00.000+00:00","History":[{"Status":"Pending","Time":"2022-04-07T08:30:44.011+00:00"}],"MyDetails":{"Query":"query1^^","name":"NEH","address":"XXX","Format":"xml","Version":"2"}}
{"_id":{"timestamp":1649320255,"date":"2022-04-07T08:30:55.000+00:00"},"ScheduleTime":"2022-04-07T09:50:00.000+00:00","History":[{"Status":"Pending","Time":"2022-04-07T08:30:55.011+00:00"}],"MyDetails":{"Query":"query2^^^","name":"ABC","address":"YYY","Format":"xml","Version":"1"}}

我需要在上面字符串中的myDetails下提取字段时,我有一个API,该API以下面的格式返回数据,然后尝试使用:

final Response response = ClientBuilder.newClient().target("url").request(MediaType.APPLICATION_JSON).get();
    JsonReader jsonReader = Json.createReader(new StringReader(response.readEntity(String.class)));
    System.out.println(jsonReader.readObject());

请让我知道如何提取字段。

I have an api which returns data in the below format when i use the clientbuilder get():

final Response response = ClientBuilder.newClient().target("url").queryParam("CustomerQuery", jsonarr).request(MediaType.APPLICATION_JSON).get();
String actual = response.readEntity(String.class);
System.out.println(actual);

Result:

{"_id":{"timestamp":1649320244,"date":"2022-04-07T08:30:44.000+00:00"},"ScheduleTime":"2022-04-07T09:50:00.000+00:00","History":[{"Status":"Pending","Time":"2022-04-07T08:30:44.011+00:00"}],"MyDetails":{"Query":"query1^^","name":"NEH","address":"XXX","Format":"xml","Version":"2"}}
{"_id":{"timestamp":1649320255,"date":"2022-04-07T08:30:55.000+00:00"},"ScheduleTime":"2022-04-07T09:50:00.000+00:00","History":[{"Status":"Pending","Time":"2022-04-07T08:30:55.011+00:00"}],"MyDetails":{"Query":"query2^^^","name":"ABC","address":"YYY","Format":"xml","Version":"1"}}

I need to extract fields under MyDetails in the above string and i tried using :

final Response response = ClientBuilder.newClient().target("url").request(MediaType.APPLICATION_JSON).get();
    JsonReader jsonReader = Json.createReader(new StringReader(response.readEntity(String.class)));
    System.out.println(jsonReader.readObject());

Please let me know how can i extract the fields.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

黑凤梨 2025-01-26 17:35:19

如果您对如何使用 JsonObject 有疑问,请阅读 Javadoc https://docs.oracle.com/javaee/7/api/javax/json/JsonObject.html

JsonReader jsonReader = Json.createReader(new StringReader(response.readEntity(String.class)));
JsonObject o = jsonReader.readObject();
JsonObject details = o.getJsonObject("MyDetails");

// details.get...

或者,使用不同的 http 客户端(例如 Retrofit),鼓励直接对象映射

If you have questions about how to use a JsonObject, read the Javadoc https://docs.oracle.com/javaee/7/api/javax/json/JsonObject.html

JsonReader jsonReader = Json.createReader(new StringReader(response.readEntity(String.class)));
JsonObject o = jsonReader.readObject();
JsonObject details = o.getJsonObject("MyDetails");

// details.get...

Alternatively, use a different http client like Retrofit that encourages direct object mapping

落日海湾 2025-01-26 17:35:19

有很多方法可以做您想做的事情。我只会描述其中的一些。您可以使用几个JSON解析库。最受欢迎的是JSON-JACKSON,也称为更快的XML(请参阅此处的链接)。您可以使用方法 readvalue objectMapper类。对于类参数,您可以使用Map.Class或自定义书面类,该类将反映JSON的结构。

gson library ,其用户指南

但如果您想要简单的解决方案,我写了自己的开源库,其中包括jonutils class,它是一个薄包装器在JSON-JACKSON库上,它为您提供了一个非常简单的解决方案。在您的情况下,可能看起来像这样(假设变量 json 字符串包含您的JSON字符串):

try {
  Map<String, Object>map = JsonUtils.readObjectFromJsonString(json, Map.class);
  Map details = map.get("MyDetails");
} catch(IOException ioe) {
...
}

Map 详细信息将包含一个带有的地图您从“ mydetails”部分中的所有键和值。如果您想在此处使用此库,请在此处获得:它称为mgntutils,您可以在使用Javadoc和源代码。它可用于

There are many ways to do what you want to do. I will just describe some of them. You can use several Json parsing libraries. The most popular ones are Json-Jackson also known as faster XML (See link here). You can use method readValue of ObjectMapper class. For class parameter you can use Map.class or your custom written class that will reflect the structure of your Json.

Than there is Gson library, with its user guide

But also if you want a simplistic solution, I wrote my own open-source library that includes JsonUtils class that is a thin wrapper over Json-Jackson library that gives you a very simple solution. In your case it may look like this (assuming variable json is a String that contains your Json string):

try {
  Map<String, Object>map = JsonUtils.readObjectFromJsonString(json, Map.class);
  Map details = map.get("MyDetails");
} catch(IOException ioe) {
...
}

Map details will contain a map with all your keys and values from section "MyDetails". If you want to use this library here is where to get it: Its called MgntUtils and you can get it on Github with Javadoc and source code. It is available as maven artifacts as well. Here is a Javadoc for JsonUtils class

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文