1

我需要根据站名“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
                }
                
            }
        } ]
}
4

2 回答 2

1

JSON嵌套Objects在一个中,Array因此您可以尝试如下所示读取温度值。我用多个温度值更新了你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 轻松从对象文档中获取值的替代方法。

    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

Maven 依赖项JsonPath

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

下面的代码片段可用于在没有任何库的情况下读取数据。

代码#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
于 2021-10-29T11:44:24.020 回答
0

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

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

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

尝试做

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

反而

于 2021-10-29T06:01:45.887 回答