Java 使用 Jackson 解析 JSON 时获取特定值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30800182/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 10:12:02  来源:igfitidea点击:

Get specific value while parsing JSON with Hymanson

javajsonHymanson

提问by Grégtheitroade Borel

I have the following JSON:

我有以下 JSON:

[  
   {  
      "A":"Lorem Ipsum ",
      "B":"Lorem Ipsum ",
      "C":"Lorem Ipsum ",
      "D":"Lorem Ipsum ",
      "E":"Lorem Ipsum ",
      "F":"Lorem Ipsum ",
      "G":301,
      "H":[  
         {  
            "Lorem Ipsum ":4,
            "Lorem Ipsum ":20,
            "Lorem Ipsum":0
         },
         {  
            "Lorem Ipsum ":5,
            "Lorem Ipsum ":19.2,
            "Lorem Ipsum ":0.8
         },
         {  
            "Lorem Ipsum ":1,
            "Lorem Ipsum ":8,
            "Lorem Ipsum ":4
         },
         {  
            "Lorem Ipsum ":3,
            "Lorem Ipsum ":14.2,
            "Lorem Ipsum ":5.8
         },
         {  
            "Lorem Ipsum ":2,
            "Lorem Ipsum ":20,
            "Lorem Ipsum ":0
         }
      ],
      "I":[  

      ],
      "J":[  

      ],
      "20-01-2014":20,
      "27-01-2014":19.2,
      "30-12-2013":8,
      "13-01-2014":14.2,
      "06-01-2014":20,
      "K":"81.40"
   },
   {  
      "reportKey":"something"
   }
]

I'd like to get the reportKeyvalue and then remove it from the file. But first I need to access it and my code doesn't seem to work:

我想获取该reportKey值,然后将其从文件中删除。但首先我需要访问它并且我的代码似乎不起作用:

final ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.readValue(rawContentParameters, JsonNode.class);

logger.info("ExportController : generatesExportExcel : parameters: {}", jsonNode.get("reportKey").textValue());

but I'm getting a java.lang.NullPointerException. Why?

但我得到了一个java.lang.NullPointerException. 为什么?

SOLUTION:

解决方案:

final ObjectMapper objectMapper = new ObjectMapper();
JsonNode rootNode = objectMapper.readValue(rawContentParameters, JsonNode.class);

logger.info("ExportController : generatesExportExcel : parameters: {}", rootNode.get(rootNode.size() - 1).get("reportKey").textValue());

采纳答案by Armand

You are accessing the root JsonNodeas if it were an object, but it's wrapped in an array. You need to extract the second object from the array before you can access reportKey:

您正在访问根JsonNode,就好像它是一个对象,但它被包装在一个数组中。您需要先从数组中提取第二个对象,然后才能访问reportKey

JsonNode array = objectMapper.readValue(rawContentParameters, JsonNode.class);
JsonNode object = array.get(1);
String reportKey = object.get("reportKey").textValue();
logger.info("ExportController : generatesExportExcel : parameters: {}", reportKey);

回答by zbig

First take second element on the list.

首先取列表中的第二个元素。

jsonNode.get(1).get("reportKey")

回答by forzagreen

You can use JSONObjectand JSONArrayfrom org.jsonlibrary :

您可以使用JSONObjectJSONArrayorg.json库:

//instantiate your json array (e.g. from a string, or a file)
//String s = "[...]";
String s = FileUtils.readFileToString(new File(yourFile));
JSONArray json = new JSONArray(s);

//get the reportKey value:
json.get(1).get("reportKey");

//removing it:
//removing all the node: {"reportKey":"something"}
json.remove(1);
//removing only "reportKey":"something" and keeping {}:
json.get(1).remove("reportKey");