简体   繁体   English

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

[英]How to extract specific data from json with multiple nodes?

  1. Using java with selenium 使用java与selenium
  2. I have captured full json in my code 我在代码中捕获了完整的json
  3. IN my json there are two nodes "Service1" and "Service2" 在我的json中有两个节点“Service1”和“Service2”
  4. My requirement is to fetch the details of "Services1" node -->data node 我的要求是获取“Services1”节点 - >数据节点的详细信息
  5. But i am not able to extract only Service1 with data node details(i do not want "test" node details. 但我无法仅提取具有数据节点详细信息的Service1(我不想要“测试”节点详细信息。

Question: Can you please help me to extract data from Service1-with data node only using java code?I am using selenium with java.I need this for one of the script. 问题:您能否帮我从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"
  }
  ]

Fetching the required data is based on the api response. 获取所需数据基于api响应。 But in the result of the fetched data you can convert it as you want in java as: 但是在获取数据的结果中,您可以在java中将其转换为:

Main theme: 主旋律:

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

Full code snippet: 完整代码段:

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

so finally in dataInFirstObject we have: 所以最后在dataInFirstObject中我们有:

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

and dataInSecondObject we have: 和dataInSecondObject我们有:

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

You can use JSON simple library ,it is library used to read/write/parse data in JSON file.In this example i read data from JSON file not a JSON content directly. 您可以使用JSON简单库 ,它是用于在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);
}

}  

If you need read JSON content from your code just replace the reader with your content like this : Object obj = parser.parse("JSON content"); 如果您需要从代码中读取JSON内容,只需将读取器替换为您的内容,如下所示: Object obj = parser.parse("JSON content");

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

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