Home > Blockchain >  How to get value of temperature based on station given?
How to get value of temperature based on station given?

Time:10-30

I need to store value of temperature as Temperature from json payload based on station name "station1" but I always ended up with null.

My code :

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 payload:

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

CodePudding user response:

Your JSON is nested Objects within an Array so you can try like below to read the temperature value. I updated your JSON with multiple temperature values to be more clear.

Updated JSON:

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

Code#1: You can use JsonPath to read the data. JsonPath is an alternative to using XPath for easily getting values from a Object document.

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

Output:

Temparature: 15.2

Maven dependency for JsonPath

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

Below code snippets can be used to read data without any libraries.

Code#2: To read all temperature values.

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

Output:

Temparature0: 13.2
Temparature1: 15.2

Code#3: Get temperature value based on station name.

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

Output:

Temparature: 15.2

CodePudding user response:

My guess is that your issue probably comes from this

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

Since you are trying to read a file that's in your classpath you need to specify where to find the file differently.

Try doing

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

instead

  • Related