Jackson JSON密钥作为Java中的值 [英] Jackson JSON key as value in Java

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

问题描述

我在Spring MVC应用程序中使用Jackson.我想使用String值作为Java POJO-> JSON

I'm using Jackson in Spring MVC application. I want to use a String value as key name for Java POJO --> JSON

"record": {
        "<Dynamic record name String>": {
          "value": { 
          ....
          }
      }
 }     

因此,动态记录名称String可以是"abcd","xyz"或任何其他字符串值.如何定义我的记录" POJO以具有这样的键?

So the dynamic record name String could be "abcd","xyz" or any other string value. How can I define my "record" POJO to have a key like that ?

推荐答案

不幸的是,Java类中不能具有 dynamic 字段(与某些其他语言不同),因此有两种选择:

Unfortunately, you cannot have dynamic fields in Java classes (unlike some other languages), so you have two choices:

  1. 使用 Map s
  2. 使用JSON对象(即 JsonNode (对于杰克逊)

假设您有一个像这样的数据:

Suppose, you have a data like this:

{
    "record": {
        "jon-skeet": {
            "name": "Jon Skeet",
            "rep": 982706
        },
        "darin-dimitrov": {
            "name": "Darin Dimitrov",
            "rep": 762173
        },
        "novice-user": {
            "name": "Novice User",
            "rep": 766
        }
    }
}

创建两个类来捕获它,一个用于用户,另一个用于对象本身:

Create two classes to capture it, one for user and another for the object itself:

User.java :

public class User {
    private String name;
    private Long rep;

    public String getName() { return name; }

    public void setName(String name) { this.name = name; }

    public Long getRep() { return rep; }

    public void setRep(Long rep) { this.rep = rep; }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", rep=" + rep +
                '}';
    }
}

Data.java :

public class Data {
    private Map<String, User> record;

    public Map<String, User> getRecord() { return record; }

    public void setRecord(Map<String, User> record) { this.record = record; }

    @Override
    public String toString() {
        return "Data{" +
                "record=" + record +
                '}';
    }
}

现在,解析JSON(我假设您的类路径的根目录中有一个data.json文件):

Now, parse the JSON (I assume there is a data.json file in the root of your classpath):

public class App {
    public static void main(String[] args) throws Exception {
        final ObjectMapper objectMapper = new ObjectMapper();

        System.out.println(objectMapper.readValue(App.class.getResourceAsStream("/data.json"), Data.class));
        System.out.println(objectMapper.readTree(App.class.getResourceAsStream("/data.json")));
    }
}

这将输出:

Data{record={jon-skeet=User{name='Jon Skeet', rep=982706}, darin-dimitrov=User{name='Darin Dimitrov', rep=762173}, novice-user=User{name='Novice User', rep=766}}}
{"record":{"jon-skeet":{"name":"Jon Skeet","rep":982706},"darin-dimitrov":{"name":"Darin Dimitrov","rep":762173},"novice-user":{"name":"Novice User","rep":766}}}

如果是Map,则可以使用某些静态类,例如User,或者通过使用Map s(Map<String, Map<String, ...>>.)中的Map s来完全动态化.您自己使用过多的地图时,请考虑切换到JsonNode.基本上,它们与Map相同,并且是专门为高度动态数据而发明"的.尽管如此,以后您将很难使用它们.

In case of a Map you can use some static classes, like User in this case, or go completely dynamic by using Maps of Maps (Map<String, Map<String, ...>>. However, if you find yourself using too much maps, consider switching to JsonNodes. Basically, they are the same as Map and "invented" specifically for highly dynamic data. Though, you'll have some hard time working with them later...

看一个完整的例子,我已经为您准备了这里.

Take a look at a complete example, I've prepared for you here.

这篇关于Jackson JSON密钥作为Java中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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