EthanAlef
EthanAlef

Reputation: 75

Parse map like json string into class by fastjson/jackson

Consider a json:

{
  "data": {
    "key1": {
      "t1":"text1",
      "t2":"text1",
    },
    "key2": {
      "t1":"text1",
      "t2":"text1",
    },
    ...
  }
  "code": 1
}

With data java class defined as below:

  @Getter
  @Setter
  public class JsonData {
    private Data data;
    private int code;
  }

  @Getter
  @Setter
  public class Data {
    private Map<String, KeyInfo> mapKeyInfo;
  }

  @Getter
  @Setter
  public class KeyInfo {
    private String t1;
    private String t2;
  }

I try to get the JsonData object containing the json content but NullPointerException:

JsonData jsonData = JSONObject.parseObject(response, JsonData.class); // NullPointerException

What should I do if I want to do in fastjson way? What if I use jackson to do the same thing?

Update

I tried one class to parse but still failed:

  @Getter
  @Setter
  public class JsonData {
    private Map<String, Map<String, String>> data;
    private int code;
  }

Upvotes: 0

Views: 694

Answers (0)

Related Questions