如何将String转换为对象并使用Jackson检索值

时间:2014-08-26 17:22:42

标签: java json rest parsing jackson

我有一个JSON请求,其形式为String: -

 {
      "insertDataRequest": [
          {
              "id": "98",
              "name": "Mercer Island",
              "age": "12",
              "designation": "SSE"
          }
      ]
  }

现在,我需要将此String格式转换为Java Object并使用Jackson ...

检索它的值

到目前为止,我做了以下事情: -

public static void main(String[] args) throws IOException 
{
    ObjectMapper objectMapper = new ObjectMapper();
    String json= "{\"insertDataRequest\":{\"id\":98, \"name\":\"Mercer Island\",\"age\":12,\"designation\":\"SEE\"}}";
    JsonNode root2 = objectMapper.readTree(json);
    JsonNode rootnode = root2.get("insertDataRequest");

    if (rootnode != null && rootnode.has("id")) {
        int id = rootnode.get("id").intValue();
        System.out.println(id);
    }     
    if (rootnode != null && rootnode.has("name")) {
        String name = rootnode.get("name").toString().replace("\"", "");
        System.out.println(name);
    }   

    if (rootnode != null && rootnode.has("age")) {
        int age = rootnode.get("age").intValue();
        System.out.println(age);
    }

    if (rootnode != null && rootnode.has("designation")) {
        String designation = rootnode.get("designation").toString().replace("\"", "");
        System.out.println(designation);
    }    
} 

现在..当我运行代码时,我没有得到任何价值....我怎样才能转换这个字符串并检索值..我错过了什么?请帮帮....

4 个答案:

答案 0 :(得分:2)

如果你使用ObjectMapper,你可以使用bean直接将你的JSON映射到你的Java对象,而不是自己动手。

对于您的代码,并根据评论:

public static void main(String[] args) throws IOException 
{
    ObjectMapper objectMapper = new ObjectMapper();
    String json= "{\"insertDataRequest\":{\"id\":98, \"name\":\"Mercer Island\",\"age\":12,\"designation\":\"SEE\"}}";
    JsonNode root2 = objectMapper.readTree(json);
    for (JsonNode rootnode : root2.get("insertDataRequest")) {
      if (rootnode.has("id")) {
          int id = rootnode.get("id").intValue();
          System.out.println(id);
      }     
      if (rootnode.has("name")) {
          String name = rootnode.get("name").toString().replace("\"", "");
          System.out.println(name);
      }   

      if (rootnode.has("age")) {
          int age = rootnode.get("age").intValue();
          System.out.println(age);
      }

      if (rootnode.has("designation")) {
          String designation = rootnode.get("designation").toString().replace("\"", "");
          System.out.println(designation);
      }    
    }
} 

JSONNode实施Iterable

答案 1 :(得分:2)

您可以将ObjectMapper配置为将JSON读取为POJO数组,并在不通过节点层次结构的情况下获取第一个数组元素。这是一个例子:

public class JacksonWrapped {
    public static final String JSON = "{\n" +
            "      \"insertDataRequest\": [\n" +
            "          {\n" +
            "              \"id\": \"98\",\n" +
            "              \"name\": \"Mercer Island\",\n" +
            "              \"age\": \"12\",\n" +
            "              \"designation\": \"SSE\"\n" +
            "          }\n" +
            "      ]\n" +
            "  }";

    public static class Bean {
        public int id;
        public String name;
        public int age;
        public String designation;

        @Override
        public String toString() {
            return "Bean{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    ", age=" + age +
                    ", designation='" + designation + '\'' +
                    '}';
        }
    }

    public static void main(String[] args) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        final Bean[] value = mapper.reader(Bean[].class)
                .withRootName("insertDataRequest")
                .readValue(JSON);
        System.out.println(value[0]);
    }
}

输出:

Bean{id=98, name='Mercer Island', age=12, designation='SSE'}

答案 2 :(得分:1)

以下是使用相同对象类型JsonNode的答案。节点本身可以​​是一个数组:

Jackson how to transform JsonNode to ArrayNode without casting?

答案 3 :(得分:0)

因此,工作解决方案与NoDataFound建议一样: -

JsonNode root2 = objectMapper.readTree(json);
    for (JsonNode rootnode : root2.get("insertDataRequest")) {
      if (rootnode.has("id")) {
          int id = rootnode.get("id").intValue();
          System.out.println(id);
      }