不使用Java对象的JSON解析(JSON parsing without using Java objects)

编程入门 行业动态 更新时间:2024-10-24 18:17:01
不使用Java对象的JSON解析(JSON parsing without using Java objects)

我想解析RESTful服务中的JSON数据。

与基于SOAP的服务不同,服务使用者可以从WSDL创建存根和框架,在RESTful服务的情况下,服务使用者获取原始JSON字符串。

由于服务使用者没有与JSON结构匹配的Java对象,因此我们无法使用JSON到Java Mappers,如GSON,Jackson等。

另一种方法是使用JsonPath,minimal-json等解析器来帮助遍历JSON结构并读取数据。

有没有更好的方法来读取JSON数据?

I want to parse JSON data from a RESTful service.

Unlike a SOAP-based service, where a service consumer can create stubs and skeleton from WSDL, in the case of the RESTful service, the service consumer gets a raw JSON string.

Since the service consumer does not have a Java object matching the JSON structure, we are not able to use the JSON to Java Mappers like GSON, Jackson etc.

One another way is to use parsers like JsonPath, minimal-json, etc which help traversing the JSON structure and read the data.

Is there any better way of reading JSON data?

最满意答案

Jackson的官方文档提到了从Java解析JSON文档的3种不同方法。 前2个不需要“与JSON结构匹配的Java对象” 。 综上所述 :

Streaming API (又名“Incremental parsing / generation”)读取和写入JSON内容为离散事件。 树模型提供JSON文档的可变内存树表示。 ObjectMapper可以构建由JsonNode节点组成的树。 数据绑定基于属性访问器约定或注释将JSON转换为POJO和从POJO转换。 通过简单的数据绑定,您可以转换为Java地图,列表,字符串,数字,布尔值和空值 使用完整数据绑定,您可以转换为任何Java bean类型(以及上面提到的“简单”类型)

另一种选择是从JSON文档生成Java Bean。 您的里程可能会有所不同,您可能/可能必须修改生成的文件。 您可以尝试使用至少5个用于此目的的在线工具:

http://www.jsonschema2pojo.org/ http://pojo.sodhanalibrary.com/ https://timboudreau.com/blog/json/read http://jsongen.byingtondesign.com/ http://json2java.azurewebsites.net/

您还可以使用IDE插件。 例如,这个为Intellij https://plugins.jetbrains.com/idea/plugin/7678-jackson-generator-plugin

The official docs for Jackson mention 3 different ways to parse a JSON doc from Java. The first 2 do not require "Java object matching the JSON structure". In Summary :

Streaming API (aka "Incremental parsing/generation") reads and writes JSON content as discrete events. Tree Model provides a mutable in-memory tree representation of a JSON document. ObjectMapper can build trees that consist of JsonNode nodes. Data Binding converts JSON to and from POJOs based either on property accessor conventions or annotations. With simple data binding you convert to and from Java Maps, Lists, Strings, Numbers, Booleans and nulls With full data binding you convert to and from any Java bean type (as well as "simple" types mentioned above)

Another option is to generate Java Beans from JSON documents. You mileage may vary and you may/probably will have to modify the generated files. There are at least 5 online tools for that purpose that you can try:

http://www.jsonschema2pojo.org/ http://pojo.sodhanalibrary.com/ https://timboudreau.com/blog/json/read http://jsongen.byingtondesign.com/ http://json2java.azurewebsites.net/

There are also IDE plugins that you can use. For instance this one for Intellij https://plugins.jetbrains.com/idea/plugin/7678-jackson-generator-plugin

更多推荐

本文发布于:2023-07-31 00:12:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1340323.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   Java   JSON   objects   parsing

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!