Java Jackson解析动态JSON [英] Java Jackson parsing dynamic JSON

查看:558
本文介绍了Java Jackson解析动态JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是杰克逊的新手,我在确定处理动态JSON文件的最佳方法时遇到了一些问题。我知道我可以使用流式传输或树API解决问题,但这会涉及很多代码,而这些代码将无法轻松维护。例如,取以下两个json文件:

I am new to Jackson and I am having some problems determining the best way to deal with processing JSON files that are dynamic in nature. I know I could solve the issue with the streaming or tree API, but this would involve a lot of code which will not be easily maintained. For example, take the following two json files:

{
   something: "somethingValue"
   somethingelse: "anotherValue"
   url: "http://something.com"
}

{
   something: "somethingValue"
   somethingelse: "anotherValue"
   url: {
           service1: [
              "http://something.com",
              "https://something.com" ],
           service2: [
              "http://something2.com",
              "https://something2.com" ],
        }
}

解析后第一个json对象的默认行为,应该将URL添加到子类URL中的service1和service2 url列表中。其中第二个允许为每个URL指定非常具体的URL。我正在计划使用的url类的数据对象如下:

the default behaviour of the first json object after being parsed, should add the URL to both service1 and service2 url lists in the subclass "URL". where the second allow specifying very specific urls to each. The data object for url class I was planning on use is as follows:

public class url {

   // ideally, I would use the java.net.URL instead of String
   public List<String> service1;    
   public List<String> service2;

   // also includes getter/setters using a fluent style
   ...
}

还有一些其他的父类会有一个URL和其他第一级json参数的参数。

There would also be some other parent class which would have a parameter for URL and other first level json parameters.

什么是在杰克逊处理这个问题的最佳方法是什么?

What is the best way to handle this in jackson?

推荐答案

答案:

在与杰克逊挣扎以简单而优雅的方式做我想做的事后,我最终切换到Gson库进行JSON解析。它允许我为我的课程创建一个非常简单的自定义反序列化器。

After struggling with Jackson to do what I want in a simple and elegant way, I ended up switching to Gson library for JSON parsing. it allowed me to create a custom deserializer for my class that was extremely easy.

我可以在这里找到类似的例子:

An example of something similar that I did can be found here:

http://www.baeldung.com/gson-deserialization-guide

我很欣赏杰克逊的帮助和指导,但这让我意识到杰克逊不会满足我的需求。

I appreciate the help and guidance with Jackson, however it just made me realize that Jackson was just not going to meet my needs.

-Stewart

这篇关于Java Jackson解析动态JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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