简体   繁体   English

如何使用Jackson JSON将值存储到Java

[英]How to store values using Jackson JSON to Java

I am having a really hard time understanding how to place a mapped valued into an array and how to iterate that array. 我很难理解如何将映射值放置到数组中以及如何迭代该数组。

test.json test.json

 [
   {
     "Name": "Bob",
     "Nationality": "",
     "City": "Chicago",
     "Phone" : "451232424234"
   },

    ......continues with more objects
 ]

testTemplate.java testTemplate.java

 //imports
  @JSONInclude(Json.Include.Non_NULL)
  @JsonPropertyOrder({"Name,"Nationality","City","Phone"})

  Public class testTemplate {

  @JsonProperty("Name")
  private String userName;

  @JsonProperty("Nationality")
  private String nation;

  @JsonProperty("City")
  private String city;

  @JsonProperty("Phone")
  private String phone;

  @JsonProperty("Name")
  public String getName (String userName) {
      this.userName = userName;
  }

  @JsonProperty("Nationality")
  public String nation (String nation) {
      this.nation = nation;
  }

  @JsonProperty("City")
  public String city (String city) {
      this.city = city;
  }

  @JsonProperty("Phone")
  public String phone (String phone) {
      this.phone = phone;
  }

  public String toString() {
   return ToStringBuilder.reflectionToString(this);
  }

testParse.java testParse.java

Public Class testParse {
   List<testParse> test;
   ObjectMapper mapper;

   protected void setUp() throws IOException {
          mapper = new ObjectMapper();
          mapper.enable(SerializationFeature.INDENT_OUTPUT();
          test = mapper.readValue(this.getClass().getResourcesAsStream("test.json"),
          mapper.getTypeFactory().constructCollectionType(List.class, testParse.class));

I need to help first clarifying exactly what the code is doing, and how to put JSON properties (Name, Nationality,City,Phone) into Java. 首先,我需要帮助您弄清楚代码在做什么,以及如何将JSON属性(名称,国籍,城市,电话)放入Java。

My understanding is the testTemplate file create the strings in which the properties will be held, then the testParse file has the mapper feature (from Jackson) that reads through the json and puts all into "test" (as an array?) 我的理解是testTemplate文件创建将在其中保存属性的字符串,然后testParse文件具有映射器功能(来自Jackson),该功能会读取json并将所有内容放入“ test”(作为数组?)

My goal is in testParse, where if everything thing is in "test", then I read through that, and start to pull it out and place it into a folderList. 我的目标是在testParse中,如果所有内容都在“ test”中,那么我将通读该内容,然后开始将其拉出并将其放入folderList。

 public static Map<String, String> userName throws IOException {
     Map<String, String> folderList = new TreeMap<>();
     //Don't know how, but iterate through "test"
     LineIterator it = new LineIterator(//the method used to read the json file);
     try {
         while(it.hasNext()) {
         String line = it.nextLine();
         folderList.put(userName,nation) //can I also put city and phone at once as well or should I create another put: folderList.put(userName, city)
         return folderList; 

How does one do this? 如何做到这一点? Is there a better way to put the properties of json into the folderList after using the jackson mapper? 使用jackson映射器后,是否有更好的方法将json的属性放入folderList?

Actually, testTemplate don't generate anything, what Jackson have done here is just get data from "test.json" as String, then using Reflection read the format of testTemplate.java. 实际上,testTemplate不会生成任何东西,Jackson在这里所做的只是从“ test.json”中以String形式获取数据,然后使用Reflection读取testTemplate.java的格式。 Based on template it've just have + setting you add to ObjectMapper object, Jackson will convert test.json to array of object Java. 根据模板,只需将+设置添加到ObjectMapper对象,Jackson会将test.json转换为对象Java数组。
P/S: you don't need to add annotation in both attributes and get function of POJO class, just do it in get function or attributes only, it's enough for Jackson. P / S:您无需在POJO类的两个属性中都添加注释并获取功能,只需在get函数或属性中进行注释,对于Jackson而言就足够了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM