【问题标题】:How to get value of temperature based on station given?如何根据给定的站点获取温度值?
【发布时间】:2021-10-29 01:54:07
【问题描述】:

我需要根据站名“station1”将来自json有效载荷的温度值存储为温度,但我总是以null结束。

我的代码:

JSONParser parser = new JSONParser()

try (Reader reader = new FileReader("src/test/resources/jsonPayload/response.json")) {


    JSONObject jsonObject = (JSONObject) parser.parse(reader);
    String response = (String) jsonObject.get("response");
    JsonPath js = new JsonPath(response);
    int size = js.getInt(“list.size()");



    List<String> station = new ArrayList<>();
    for (int i = 0; i < size; i++) {
        station.add(js.getString("list[$i].station.station_name))
  
    def Temperature
Temperature = new JsonSlurper().parseText(response).list.station.station_name=["station1”].obs.temp.temprature
}

json 负载:

{
    "list": [
        {
            "station": {
                "identity": {
                    "station_name": "station1"
                }
            },
            "obs": {
                "temp": {
                    "temprature": 13.2
                }
                
            }
        } ]
}

【问题讨论】:

    标签: java selenium jsonslurper


    【解决方案1】:

    您的JSON 嵌套在ObjectsArray,因此您可以尝试如下所示读取温度值。我用多个温度值更新了你的JSON,以便更清楚。

    更新的 JSON

    {
        "list": [
            {
                "station": {
                    "identity": {
                        "station_name": "station1"
                    }
                },
                "obs": {
                    "temp": {
                        "temprature": 13.2
                    }
                }
            },
            {
                "station": {
                    "identity": {
                        "station_name": "station2"
                    }
                },
                "obs": {
                    "temp": {
                        "temprature": 15.2
                    }
                }
            }
        ]
    }
    

    Code#1:可以使用JsonPath读取数据。 JsonPath 是使用 XPath 轻松从 Object 文档中获取值的替代方法。

        File jsonFile = new File("Sample.json");
        JSONArray jsonArray = new JSONArray(JsonPath.parse(jsonFile).read("list").toString());
    
        for (int i = 0; i < jsonArray.length(); i++) {
            String stationName = JsonPath.parse(jsonFile).read("list[" + i + "].station.identity.station_name")
                    .toString();
            if (stationName.equalsIgnoreCase("station2")) {
                String temparature = JsonPath.parse(jsonFile).read("list[" + i + "].obs.temp.temprature").toString();
                System.out.println("Temparature: "+temparature);
            }
        }
    

    输出

    Temparature: 15.2
    

    JsonPath 的 Maven 依赖项

        <dependency>
            <groupId>com.jayway.jsonpath</groupId>
            <artifactId>json-path</artifactId>
            <version>0.9.0</version>
        </dependency>
    

    下面的代码sn-ps可以在没有任何库的情况下读取数据。

    代码#2:读取所有温度值。

            String jsonDataAsString = new String(Files.readAllBytes(Paths.get("Sample.json")));
            JSONObject jsonObject = new JSONObject(jsonDataAsString);
            JSONArray jsonArray = new JSONArray(jsonObject.get("list").toString());
    
            for (int i = 0; i < jsonArray.length(); i++) {
                jsonObject = new JSONObject(jsonArray.get(i).toString());
                jsonObject = new JSONObject(jsonObject.get("obs").toString());
                jsonObject = new JSONObject(jsonObject.get("temp").toString());
                System.out.println("Temparature" + i + ": " + jsonObject.get("temprature"));
            }
    

    输出

    Temparature0: 13.2
    Temparature1: 15.2
    

    代码#3:根据站名获取温度值。

            String jsonDataAsString = new String(Files.readAllBytes(Paths.get("Sample.json")));
            JSONObject jsonTemparatureObject;
            JSONObject jsonObject = new JSONObject(jsonDataAsString);
            JSONArray jsonArray = new JSONArray(jsonObject.get("list").toString());
    
            for (int i = 0; i < jsonArray.length(); i++) {
                jsonObject = new JSONObject(jsonArray.get(i).toString());
                jsonTemparatureObject = new JSONObject(jsonObject.get("obs").toString());
                jsonObject = new JSONObject(jsonObject.get("station").toString());
                jsonObject = new JSONObject(jsonObject.get("identity").toString());
    
                if (jsonObject.get("station_name").toString().equalsIgnoreCase("station2")) {
                    jsonObject = new JSONObject(jsonTemparatureObject.get("temp").toString());
                    System.out.println("Temparature: " + jsonObject.get("temprature"));
                }
            }
    

    输出

    Temparature: 15.2
    

    【讨论】:

      【解决方案2】:

      我的猜测是你的问题可能来自这个

      try (Reader reader = new FileReader("src/test/resources/jsonPayload/response.json"))
      

      由于您尝试读取 classpath 中的文件,因此您需要指定以不同方式查找文件的位置。

      尝试做

      try (Reader reader = new FileReader("jsonPayload/response.json"))
      

      改为

      【讨论】:

      • 没有。从文件中读取是正确的。我的要求是我需要根据站名存储温度变量所以假设我有多个温度值并且我需要为 station_name="station1" 存储温度值
      猜你喜欢
      • 2022-11-27
      • 2016-04-10
      • 2023-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-09
      相关资源
      最近更新 更多