【问题标题】:How to convert a String into object and retrieve the value using Jackson如何将字符串转换为对象并使用杰克逊检索值
【发布时间】:2014-10-20 02:58:55
【问题描述】:

我有一个字符串形式的 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);
    }    
} 

现在..我在运行代码时没有得到任何值..如何转换这个字符串并检索值..我错过了什么???请帮忙....

【问题讨论】:

  • 使用你的调试器,检查变量。请注意,insertDataRequest 属性是一个对象的 array。不是对象。
  • 如果我从请求中删除 '[' 我能够获取值 .. 但我需要 '[' ...因为值可以重复 .. 我怎样才能使用 '[ ' ...
  • 阅读JsonNode的javadoc。找到允许在索引 0 处获取元素的方法。fasterxml.github.io/jackson-databind/javadoc/2.3.0/com/…
  • 我已经搜索了整个网络..但没有找到任何合适的方法来迭代给定格式的请求..请帮助我获得迭代和获取值的方法跨度>
  • 那是你的错误。你不应该在网上到处搜索。您应该阅读 JsonNode 的 javadoc 以了解如何使用它,并访问索引 0 处的元素。

标签: java json rest parsing jackson


【解决方案1】:

如果您使用 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。

【讨论】:

  • 稍微更正.. 它作为 JsonNode rootnode 工作:root2.get("insertDataRequest") 而不是 JsonNode rootnode = root2.get("insertDataRequest") ... 需要删除 '=' 与':' 它工作正常.. 谢谢
【解决方案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'}

【讨论】:

    【解决方案3】:

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

    Jackson how to transform JsonNode to ArrayNode without casting?

    【讨论】:

      【解决方案4】:

      因此,工作解决方案正如 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);
            }     
      

      【讨论】:

        猜你喜欢
        • 2017-10-03
        • 1970-01-01
        • 1970-01-01
        • 2012-11-15
        • 2017-09-22
        • 1970-01-01
        • 1970-01-01
        • 2017-12-03
        相关资源
        最近更新 更多