user1798578 user1798578 - 7 months ago 27
Java Question

How to Parse JSON Response in JAVA

Im having trouble retrieving the temperature from the json code below using java. I'm able to retrieve the location but when I try to get the temperature i'm receiving the error

JSONObject["temp"] not a string.

Can someone please help me with this problem.

URL url1 = new URL(urlJson);
connection = (HttpURLConnection)url1.openConnection();
connection.setRequestProperty("Accept-Charset", charset);

responseCode = connection.getResponseCode();

InputStream response = connection.getInputStream();
StringWriter writer = new StringWriter();
IOUtils.copy(response, writer, "utf-8");
String theString = writer.toString();
System.out.println("THE STRING"+theString);

JSONObject obj = new JSONObject(theString);

String location = obj.getString("name");
System.out.println("The location is -"+location);

String temp = obj.getString("temp");
System.out.println("The current temperature is -"+temp);

JSON code

"description":"sky is clear",


You need to change your function to .getDouble temp is not returning as a string value type.

So ıt should be like:


I edit the answer.