Java 如何将字符串转换为对象并使用Jackson检索值

Java 如何将字符串转换为对象并使用Jackson检索值,java,json,rest,parsing,jackson,Java,Json,Rest,Parsing,Jackson,我有一个字符串形式的Json请求:- { "insertDataRequest": [ { "id": "98", "name": "Mercer Island", "age": "12", "designation": "SSE" } ] } 现在,我需要将这个字符串格式转换成Java对象,并使用Jackson检

我有一个字符串形式的Json请求:-

 {
      "insertDataRequest": [
          {
              "id": "98",
              "name": "Mercer Island",
              "age": "12",
              "designation": "SSE"
          }
      ]
  }
现在,我需要将这个字符串格式转换成Java对象,并使用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);
    }    
} 

现在。。当我运行代码时,我没有得到任何值…如何转换此字符串并检索值。。我是不是错过了什么???请帮忙

如果您使用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);
      }    
    }
} 

实现Iterable。

如果您使用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);
      }    
    }
} 

实现Iterable。

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


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

您可以将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'}
您可以将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'}

因此,工作解决方案如下所示:-

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);
      }     

因此,工作解决方案如下所示:-

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);
      }     

使用调试器并检查变量。请注意,insertDataRequest属性是一个对象的数组。不是对象。如果我从请求中删除“[”,我可以获得值。但是我需要“[”,因为值可以重复。我如何使用['…阅读JsonNode的javadoc。找到允许获取索引0处元素的方法。我已经搜索了整个网络..但没有找到任何合适的方法以给定格式迭代请求..请帮助我获取迭代和获取值的方法这是您的错误。您不应该搜索整个网络。您应该阅读他使用JsonNode的javadoc来理解如何使用它,并访问索引0处的元素。使用调试器并检查变量。请注意,insertDataRequest属性是一个对象的数组,而不是一个对象。如果从请求中删除“[”,我可以获得值。但我需要”['…因为这些值可以重复..如何使用['…阅读JsonNode的javadoc。找到允许获取索引0处元素的方法。我已经搜索了整个网络..但没有找到任何合适的方法以给定格式迭代请求..请帮助我获取迭代和获取值的方法这是您的错误。您不应该搜索整个网络。您应该阅读他使用JsonNode的javadoc来理解如何使用它,并访问索引0处的元素。一个轻微的更正..它作为JsonNode rootnode:root2.getinsertDataRequest工作,而不是JsonNode rootnode=root2.getinsertDataRequest…需要删除带有“:”的“=”并且它工作正常..谢谢你一个轻微的更正..它作为JsonN工作ode rootnode:root2.getinsertDataRequest而不是JsonNode rootnode=root2.getinsertDataRequest…需要删除带有“:”的“=”并且它工作正常..谢谢