如何从API中提取JSON字段
当我使用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());
请让我知道如何提取字段。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您对如何使用 JsonObject 有疑问,请阅读 Javadoc https://docs.oracle.com/javaee/7/api/javax/json/JsonObject.html
或者,使用不同的 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
Alternatively, use a different http client like Retrofit that encourages direct object mapping
有很多方法可以做您想做的事情。我只会描述其中的一些。您可以使用几个JSON解析库。最受欢迎的是JSON-JACKSON,也称为更快的XML(请参阅此处的链接)。您可以使用方法 readvalue objectMapper类。对于类参数,您可以使用Map.Class或自定义书面类,该类将反映JSON的结构。
比 gson library ,其用户指南,
但如果您想要简单的解决方案,我写了自己的开源库,其中包括jonutils class,它是一个薄包装器在JSON-JACKSON库上,它为您提供了一个非常简单的解决方案。在您的情况下,可能看起来像这样(假设变量
json
是字符串
包含您的JSON字符串):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 aString
that contains your Json string):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