简体   繁体   English

使用数组从 JSON 创建 POJO model

[英]Creating a POJO model from a JSON with an array

I am trying to create a java model for a JSON structure that looks like this:我正在尝试为 JSON 结构创建一个 java model,如下所示:

"notification": {
      "field": "string",           
      "object": [
      {
        "field2": "string",
        "object2": [
          {
            "field3": "string",
            "object3": {
              "code": "string",
              "proprietary": "string"
            }
          }
        ]
      }
    ]
 }

So far my model looks like this:到目前为止,我的 model 看起来像这样:

public class Notification {

      private String field = "sample value";
      public List<object> sampleObject= new ArrayList<object>();

        public String getField() {
            return field;
        }

        public void setField(String notifyingParticipantfinancialInstitutionBIC) {
            this.field = field;
        }

        public List<object> getObject() {
            return object;
        }

        public void setObject(List<object> sampleObject) {
            this.sampleObject = sampleObject;
        }
    }
    
    public class object{

        public String field2 = "string";
        public List<object2> sampleObject2 = null;

        public String getField2() {
            return field2;
        }

        public void setField2(String field2) {
            this.field2 = field2;
        }

        public List<object2> getObject2() {
            return object2;
        }

        public void setObject2(List<object2> sampleObject2) {
            this.object2 = object2;
        }
    }


I have populated some of the existing fields just to make sure I can print back a json from this POJO model. When I print it using:我已经填充了一些现有字段,以确保我可以从这个 POJO model 打印回 json。当我打印它时使用:

ObjectMapper mapper = new ObjectMapper();
            Notification test = new Notification();
            String jsonInString = mapper.writeValueAsString(test);

i get this JSON object:我得到这个 JSON object:

"notification": {
        "field": "string",
        "object": []
    }

What am I doing wrong with my 1st object class?我的第一个 object class 做错了什么? I'm not sure why the List is not populating all the other nested variables and objects within it, but returning an empty array.我不确定为什么 List 没有填充其中的所有其他嵌套变量和对象,而是返回一个空数组。

//// ////

Just fixed it thanks to pleft and Nagaraju Chitimilla's correction.由于 pleft 和 Nagaraju Chitimilla 的更正,刚刚修复了它。 I had to add the new objects to my main object.我必须将新对象添加到我的主要对象中。

Did you created and added object and object2?您是否创建并添加了 object 和 object2?

eg例如

 Notification test = new Notification();
object obj =new object();
List list = new ArrayList<>();
list.add(obj);
test.setObject(list);

You need to add some objects in your list!您需要在列表中添加一些对象!

eg例如

Notification test = new Notification();
test.getObject().add(new object()); //Add a new object in your object list

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

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