简体   繁体   English

使用 Jackson 将 JSON 字符串解析为数组

[英]Parse a JSON string to an array using Jackson

I get the following JSON response:我得到以下 JSON 响应:

[
{
    "name": "Assets",
    "id": "assets---0020",
    "elements": [
        {
            "name": "user generates an token",
            "steps": [
            {
                "name": "I have generated a common access token",
                "result": {
                    "duration": 1124800,
                    "status": "passed"
                }                    
            }
            ]
        },
        {
            "name": "user generates an token",
            "steps": [
            { 
                "name": "I have generated a common access token",
                "result": {
                    "duration": 1124800,
                    "status": "passed"
                }                    
            }
            ]
        },
        {
            "name": "User clicks on dropdown",
            "steps": [
            {
                "name": "User clicks on dropdown and selects option and api response printed",
                "result": {
                    "duration": 1124800,
                    "status": "failed"
                }
            }
            ]
        }
    ]    
}
]

I want below output:我想要以下输出:

Name: Assets
Total - 3
No Of Success - 2
No Of Failed - 1

I've been trying to parse it automatically with Jackson with little/no success.我一直试图用杰克逊自动解析它,但很少/没有成功。 I am able to display:我能够显示:

Name: Assets 

below is code for my main Java class:下面是我的主要 Java 类的代码:

package JsonToJava.test;

import java.io.File;
import java.io.IOException;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;

import com.fasterxml.jackson.databind.ObjectMapper;


public class JsontoJava
{

    public static void main( String[] args ) throws JsonMappingException, JsonProcessingException
    {        
       ObjectMapper mapper = new ObjectMapper();

       try {            
      
          Data[] dataObj  = mapper.readValue(new File("data/sample_array_data.json"), Data[].class);
       
      
          for (Data data: dataObj) {
           
             System.out.println("Track Name--->"+data.getName());
            
             System.out.println("Printing Elements--->"+data.getElements());
            
             System.out.println("------------------------");
          }
        } catch (JsonMappingException e) {
          e.printStackTrace();
        }catch (JsonProcessingException e) {
          e.printStackTrace();
        }catch (IOException e) {
          e.printStackTrace();
      } 
   }
}

But not getting how to loop elements & get desired data from it.但没有了解如何循环元素并从中获取所需的数据。 Anyone know what is the solution?有谁知道解决方案是什么? Please help.请帮忙。

Click this link jsonschema2pojo and paste your JSON code.单击此链接jsonschema2pojo并粘贴您的 JSON 代码。 Type the name of your file and package and select both the radio buttons JSON and jackson 2.x .输入文件和包的名称,然后选择单选按钮JSONjackson 2.x Click preview and copy the code displayed in the modal window.单击预览并复制模态窗口中显示的代码。

Screenshot截屏

jsonschema2pok

So you have a list of JSON objects ( in the question this list contains only one element) and you have defined Data class that can map this single json object, right?所以你有一个 JSON 对象列表(在问题中这个列表只包含一个元素)并且你已经定义了可以映射这个单个 json 对象的Data类,对吗?

In this case this question is actually about how to parse a list of object with jackson:在这种情况下,这个问题实际上是关于如何使用 jackson 解析对象列表:

Here is an answer:这是一个答案:

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

...
String str = "...";
List<Data> dataList = objectMapper.readValue(str, new TypeReference<List<Data>>(){});

Do this Data[] datas = mapper.readValue(new File("sample_json_file.json"), Data[].class);这样做Data[] datas = mapper.readValue(new File("sample_json_file.json"), Data[].class); then loop through.然后循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM