如何从多个节点的json中提取特定数据?

时间:2018-03-24 19:15:31

标签: java json jsonparser

  1. 将java与selenium一起使用
  2. 我在代码中捕获了完整的json
  3. 在我的json中有两个节点" Service1"和" Service2"
  4. 我的要求是获取" Services1"的详细信息。节点 - >数据节点
  5. 但我无法仅提取带有数据节点详细信息的Service1(我不想"测试"节点详细信息。
  6. 问题:你能帮助我从Service1中提取数据 - 仅使用java代码获取数据节点吗?我正在使用selenium和java.I需要这个脚本之一。

     Actual response:
        {
    "Service1": [
        {
            "data": {
                "status": false,
                "type": "A",
                "order": 1,
                "Version": "v6"
            },
            "test": {
                "ModuleID": "123456",
                "BoxID": "a777",
                "Url": "https://google.com"
    
            }
    
        },
        {
           "data": {
                "status": true,
                "type": "B",
                "order": 1,
                "Version": "v2"
            },
            "test": {
                "ModuleID": "123456",
                "BoxID": "a777",
                "Url": "https://google.com"
    
    
            }
        }
    
    ],
    "Service2": {
       "data1": {
                "status": false,
                "type": "c",
                "order": 1,
                "Version": "v6"
            },
            "dashboard": {
                "ModuleID": "123456",
                "BoxID": "a777",
                "Url": "https://google.com"
    
            }
    }
    }
    
    Expected data :
    [
      {
        "status": false,
        "type": "A",
        "order": 1,
        "Version": "v6"
       },
      {
       "status": true,
       "type": "B",
       "order": 1,
       "Version": "v2"
      }
      ]
    

2 个答案:

答案 0 :(得分:2)

获取所需数据基于api响应。但是在获取数据的结果中,您可以在java中将其转换为:

主题:

JSONObject response = new JSONObject("json data");//replace json data with your given json data
JSONArray service1 = response.getJSONArray("Service1");
JSONObject firstObjectInService1 = service1.getJSONObject(0);
JSONObject secondObjectInService1 = service1.getJSONObject(1);
JSONObject dataInFirstObject = firstObjectInService1.getJSONObject("data");
JSONObject dataInSecondObject = secondObjectInService1.getJSONObject("data");

完整的代码段:

import org.json.*;
public class JsonParsing {
public static void main(String[] args) {
    String jsonData = "{\n" +
            "\"Service1\": [\n" +
            "    {\n" +
            "        \"data\": {\n" +
            "            \"status\": false,\n" +
            "            \"type\": \"A\",\n" +
            "            \"order\": 1,\n" +
            "            \"Version\": \"v6\"\n" +
            "        },\n" +
            "        \"test\": {\n" +
            "            \"ModuleID\": \"123456\",\n" +
            "            \"BoxID\": \"a777\",\n" +
            "            \"Url\": \"https://google.com\"\n" +
            "\n" +
            "        }\n" +
            "\n" +
            "    },\n" +
            "    {\n" +
            "       \"data\": {\n" +
            "            \"status\": true,\n" +
            "            \"type\": \"B\",\n" +
            "            \"order\": 1,\n" +
            "            \"Version\": \"v2\"\n" +
            "        },\n" +
            "        \"test\": {\n" +
            "            \"ModuleID\": \"123456\",\n" +
            "            \"BoxID\": \"a777\",\n" +
            "            \"Url\": \"https://google.com\"\n" +
            "\n" +
            "\n" +
            "        }\n" +
            "    }\n" +
            "\n" +
            "],\n" +
            "\"Service2\": {\n" +
            "   \"data1\": {\n" +
            "            \"status\": false,\n" +
            "            \"type\": \"c\",\n" +
            "            \"order\": 1,\n" +
            "            \"Version\": \"v6\"\n" +
            "        },\n" +
            "        \"dashboard\": {\n" +
            "            \"ModuleID\": \"123456\",\n" +
            "            \"BoxID\": \"a777\",\n" +
            "            \"Url\": \"https://google.com\"\n" +
            "\n" +
            "        }\n" +
            "}\n" +
            "}";
    System.out.println(jsonData);

    try {
        JSONObject response = new JSONObject(jsonData);//replace json data with your given json data
        JSONArray service1 = response.getJSONArray("Service1");
        JSONObject firstObjectInService1 = service1.getJSONObject(0);
        JSONObject secondObjectInService1 = service1.getJSONObject(1);
        JSONObject dataInFirstObject = firstObjectInService1.getJSONObject("data");
        JSONObject dataInSecondObject = secondObjectInService1.getJSONObject("data");
        System.out.println(dataInFirstObject);
        System.out.println(dataInSecondObject);
    } catch (JSONException je) {
        //do what you want
    }
}
}

所以最后在dataInFirstObject中我们有:

{
   "status": false,
   "type": "A",
   "order": 1,
   "Version": "v6" 
}
我们有

和dataInSecondObject:

{
    "status": true, 
    "type": "B",
    "order": 1,
    "Version": "v2"
}

答案 1 :(得分:1)

您可以使用JSON simple library,它是用于在JSON文件中读取/写入/解析数据的库。在此示例中,我直接从JSON文件读取数据而不是JSON内容。

 private void findData() {

   ArrayList<String> dataArrayList=new ArrayList<>();

try {

    JSONParser parser = new JSONParser();
    InputStream in = getClass().getResourceAsStream("/json/Services.json");
    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
    Object obj = parser.parse(reader);
    JSONObject jsonObject = (JSONObject) obj;

    JSONArray msg = (JSONArray) jsonObject.get("Service1");
    Iterator<JSONObject> iterator = msg.iterator();

    while (iterator.hasNext()) {
        JSONObject jsonObjectData = iterator.next();
        JSONObject dataObject = (JSONObject) jsonObjectData.get("data");
        System.out.println(dataObject); 
        //Extract values 
  dataObject.values().stream().forEach(value->{
                    System.out.println(value);
     dataArrayList.add(value.toString());

                });
    }

} catch (IOException | org.json.simple.parser.ParseException ex) {
    Logger.getLogger(CorporationRegistrationFormController.class.getName()).log(Level.SEVERE, null, ex);
}

}  

如果您需要阅读代码中的JSON内容,只需将阅读器替换为您的内容,如下所示:Object obj = parser.parse("JSON content");