1
  1. 将 java 与 selenium 一起使用
  2. 我在我的代码中捕获了完整的 json
  3. 在我的 json 中有两个节点“Service1”和“Service2”
  4. 我的要求是获取“Services1”节点的详细信息——>数据节点
  5. 但我无法仅提取带有数据节点详细信息的 Service1(我不想要“测试”节点详细信息。

问题:您能帮我从 Service1 中提取数据吗?仅使用 java 代码从带有数据节点的数据节点中提取数据?我正在使用 selenium 和 java。我需要这个脚本之一。

 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"
  }
  ]
4

2 回答 2

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"
}
于 2018-03-24T19:49:13.873 回答
1

您可以使用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);
}

}  

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

于 2018-03-24T20:01:28.260 回答