1

This is the JSON Response I received from OpenWeatherMap:

{
  "coord": {
    "lon": 85.84,
    "lat": 20.26
  },
  "weather": [
    {
      "id": 721,
      "main": "Haze",
      "description": "haze",
      "icon": "50d"
    }
  ],
  "base": "stations",
  "main": {
    "temp": 305.15,
    "pressure": 1007,
    "humidity": 70,
    "temp_min": 305.15,
    "temp_max": 305.15
  },
  "visibility": 5000,
  "wind": {
    "speed": 4.1,
    "deg": 170
  },
  "clouds": {
    "all": 20
  },
  "dt": 1551686400,
  "sys": {
    "type": 1,
    "id": 9113,
    "message": 0.0037,
    "country": "IN",
    "sunrise": 1551659668,
    "sunset": 1551702153
  },
  "id": 1275817,
  "name": "Bhubaneswar",
  "cod": 200
}

While I am able to use Weather by using this code below but unable to convert main to array. Is there anyway I can convert the bolded response to array or String.

JSONObject jsonObject = new JSONObject(result);
            String weatherInfo = jsonObject.getString("weather");

            JSONArray arr = new JSONArray(weatherInfo);




            for(int i=0;i<arr.length();i++)
            {
                JSONObject jsonPart = arr.getJSONObject(i);
                String main = jsonPart.getString("main");
                String description =jsonPart.getString("description");

                if(main !="" && description !="")
                {
                    message += main + " : " + description + "\r\n";

                }


            }
1
  • 2
    Changed the tag from [android-studio] to [android] since the question doesn't appear to have anything to do with the IDE specifically. Commented Mar 4, 2019 at 9:12

2 Answers 2

1

The suggestion is, don't parse the JSON by hand. Use third party library like Gson to parse the JSON. This way you won't run into any typo errors and let the library handle the parsing for you.

Sign up to request clarification or add additional context in comments.

Comments

0

Paste your json string to this website, and generate your pojo. Use the Gson to parse your string and bind to generated pojo.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.