0

I'm trying to map the following json via jackson to a pojo. I have the first part working (up until section). However, the second part I'm not sure how I can map "section1" to perhaps a contained pojo or best practice here?

json received via rest call:

{
   "val1":100,
   "val2":2.75,
   "junk":[
      {
         "junk1":36,
         "junk2":78
      },
      {
         "junk1":36,
         "junk2":78
      }
   ],
   "section1":{     // <- There will be another section2, section3,...
      "val1":100,
      "val2":2.75,
      "junk1":[
         {
            "junk1":36,
            "junk2":78
         },
         {
            "junk1":36,
            "junk2":78
         }
      ],
      "junk2":[
         {
            "junk1":36,
            "junk2":78
         },
         {
            "junk1":36,
            "junk2":78
         }
      ]
   }
}

Pojo:

public class view
{
    private int val1;
    private float val2;
    private List<Map<String, String> > junk; //<-Ok as I just pass to another class

        // How to store "section" ... and want to keep junk1, junk2 same
        // placeholder like I have for junk in main section above.


}
4

2 回答 2

1

如果每个部分(和每块垃圾)的内容在内部具有相同的结构,则将“部分”设为另一个类,并将“垃圾”设为“垃圾”。

public class view
{
    private int val1;
    private float val2;
    private List<Junk> junk; //<-Ok as I just pass to another class
    private Section section1;
    private Section section2;
    ... etc ...
}

public class Section {
   private int val1;
    private float val2;
    private List<Junk> junk; //<-Ok as I just pass to another class
}

public class Junk {
   private String junk1;
   private Strign junk2;
}

我假设您显示的结构而不是一系列部分。

另外,地图不是存储两个随机字符串的好地方。但杰克逊会允许的。唯一的例外是如果字段的名称与“junk1”和“junk2”不同。否则,只需使用一个包含两个字符串的类。

于 2013-08-13T16:27:34.190 回答
1

请注意,您可以指示应忽略“垃圾”。有几种方法可以做到这一点;一是做:

@JsonIgnoreProperties({"junk"})
public class MyClass { ... }

这样做的好处是不需要内存来存储匹配的数据;并且它可以加快解析速度(不是很多,而是一些;解析器仍然需要解码 JSON 以跳过它等)。

于 2013-08-13T22:59:01.043 回答