Reputation: 1024
I am trying to implement city search API and getting the JSON
Response correctly but not being able to parse the JSON
.
I am having the log as->
12-19 13:47:24.262 16260-16260/com.example.findloc W/System.err: at org.json.JSON.typeMismatch(JSON.java:111)
12-19 13:47:24.262 16260-16260/com.example.findloc W/System.err: at org.json.JSONObject.<init>(JSONObject.java:160)
12-19 13:47:24.262 16260-16260/com.example.findloc W/System.err: at org.json.JSONObject.<init>(JSONObject.java:173)
12-19 13:47:24.263 16260-16260/com.example.findloc W/System.err: at com.example.findloc.MainActivity.parseJson(MainActivity.java:65)
12-19 13:47:24.263 16260-16260/com.example.findloc W/System.err: at com.example.findloc.MainActivity.access$100(MainActivity.java:19)
12-19 13:47:24.263 16260-16260/com.example.findloc W/System.err: at com.example.findloc.MainActivity$FetchWeatherResponse.onPostExecute(MainActivity.java:55)
12-19 13:47:24.263 16260-16260/com.example.findloc W/System.err: at com.example.findloc.MainActivity$FetchWeatherResponse.onPostExecute(MainActivity.java:30)
12-19 13:47:24.263 16260-16260/com.example.findloc W/System.err: at android.os.AsyncTask.finish(AsyncTask.java:667)
12-19 13:47:24.263 16260-16260/com.example.findloc W/System.err: at android.os.AsyncTask.-wrap1(AsyncTask.java)
12-19 13:47:24.263 16260-16260/com.example.findloc W/System.err: at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:684)
12-19 13:47:24.263 16260-16260/com.example.findloc W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
12-19 13:47:24.263 16260-16260/com.example.findloc W/System.err: at android.os.Looper.loop(Looper.java:154)
12-19 13:47:24.263 16260-16260/com.example.findloc W/System.err: at android.app.ActivityThread.main(ActivityThread.java:6195)
12-19 13:47:24.263 16260-16260/com.example.findloc W/System.err: at java.lang.reflect.Method.invoke(Native Method)
12-19 13:47:24.263 16260-16260/com.example.findloc W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
12-19 13:47:24.263 16260-16260/com.example.findloc W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)
12-19 13:47:27.927 16260-16260/com.example.findloc V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@6e3b9d3
My code for parsing the JSON is->
try {
JSONObject jobj=new JSONObject(s);
Weather wthr=new Weather();
String key=jobj.getString("Key");
wthr.setKey(key);
Toast.makeText(getApplicationContext(),key,Toast.LENGTH_SHORT).show();
String country=jobj.getJSONObject("Country").getString("EnglishName");
wthr.setCountry(country);
Toast.makeText(getApplicationContext(),country,Toast.LENGTH_SHORT).show();
Log.i("MainActivity","Key:"+key);
Log.i("MainActivity","Country:"+country);
} catch (JSONException e) {
e.printStackTrace();
}
How should I go ahead to parse a JSON
of this type?? There is only a single object and have no name. How to parse this JSON
?
Here is the complete JSON
response I got from the URL..
[
{
"Version": 1,
"Key": "2880633",
"Type": "City",
"Rank": 85,
"LocalizedName": "Agarpara",
"EnglishName": "Agarpara",
"PrimaryPostalCode": "",
"Region": {
"ID": "ASI",
"LocalizedName": "Asia",
"EnglishName": "Asia"
},
"Country": {
"ID": "IN",
"LocalizedName": "India",
"EnglishName": "India"
},
"AdministrativeArea": {
"ID": "WB",
"LocalizedName": "West Bengal",
"EnglishName": "West Bengal",
"Level": 1,
"LocalizedType": "State",
"EnglishType": "State",
"CountryID": "IN"
},
"TimeZone": {
"Code": "IST",
"Name": "Asia/Kolkata",
"GmtOffset": 5.5,
"IsDaylightSaving": false,
"NextOffsetChange": null
},
"GeoPosition": {
"Latitude": 22.295,
"Longitude": 87.337,
"Elevation": {
"Metric": {
"Value": 27,
"Unit": "m",
"UnitType": 5
},
"Imperial": {
"Value": 88,
"Unit": "ft",
"UnitType": 0
}
}
},
"IsAlias": false,
"SupplementalAdminAreas": [
{
"Level": 2,
"LocalizedName": "Paschim Medinipur",
"EnglishName": "Paschim Medinipur"
},
{
"Level": 3,
"LocalizedName": "Kharagpur - I",
"EnglishName": "Kharagpur - I"
}
],
"DataSets": []
},
{
"Version": 1,
"Key": "2877395",
"Type": "City",
"Rank": 85,
"LocalizedName": "Agarpara",
"EnglishName": "Agarpara",
"PrimaryPostalCode": "",
"Region": {
"ID": "ASI",
"LocalizedName": "Asia",
"EnglishName": "Asia"
},
"Country": {
"ID": "IN",
"LocalizedName": "India",
"EnglishName": "India"
},
"AdministrativeArea": {
"ID": "WB",
"LocalizedName": "West Bengal",
"EnglishName": "West Bengal",
"Level": 1,
"LocalizedType": "State",
"EnglishType": "State",
"CountryID": "IN"
},
"TimeZone": {
"Code": "IST",
"Name": "Asia/Kolkata",
"GmtOffset": 5.5,
"IsDaylightSaving": false,
"NextOffsetChange": null
},
"GeoPosition": {
"Latitude": 22.472,
"Longitude": 87.442,
"Elevation": {
"Metric": {
"Value": 14,
"Unit": "m",
"UnitType": 5
},
"Imperial": {
"Value": 45,
"Unit": "ft",
"UnitType": 0
}
}
},
"IsAlias": false,
"SupplementalAdminAreas": [
{
"Level": 2,
"LocalizedName": "Paschim Medinipur",
"EnglishName": "Paschim Medinipur"
},
{
"Level": 3,
"LocalizedName": "Midnapore",
"EnglishName": "Midnapore"
}
],
"DataSets": []
},
{
"Version": 1,
"Key": "2883197",
"Type": "City",
"Rank": 85,
"LocalizedName": "Agarpara",
"EnglishName": "Agarpara",
"PrimaryPostalCode": "",
"Region": {
"ID": "ASI",
"LocalizedName": "Asia",
"EnglishName": "Asia"
},
"Country": {
"ID": "IN",
"LocalizedName": "India",
"EnglishName": "India"
},
"AdministrativeArea": {
"ID": "WB",
"LocalizedName": "West Bengal",
"EnglishName": "West Bengal",
"Level": 1,
"LocalizedType": "State",
"EnglishType": "State",
"CountryID": "IN"
},
"TimeZone": {
"Code": "IST",
"Name": "Asia/Kolkata",
"GmtOffset": 5.5,
"IsDaylightSaving": false,
"NextOffsetChange": null
},
"GeoPosition": {
"Latitude": 22.129,
"Longitude": 87.217,
"Elevation": {
"Metric": {
"Value": 33,
"Unit": "m",
"UnitType": 5
},
"Imperial": {
"Value": 108,
"Unit": "ft",
"UnitType": 0
}
}
},
"IsAlias": false,
"SupplementalAdminAreas": [
{
"Level": 2,
"LocalizedName": "Paschim Medinipur",
"EnglishName": "Paschim Medinipur"
},
{
"Level": 3,
"LocalizedName": "Keshiary",
"EnglishName": "Keshiary"
}
],
"DataSets": []
},
{
"Version": 1,
"Key": "2883968",
"Type": "City",
"Rank": 85,
"LocalizedName": "Agarpara",
"EnglishName": "Agarpara",
"PrimaryPostalCode": "",
"Region": {
"ID": "ASI",
"LocalizedName": "Asia",
"EnglishName": "Asia"
},
"Country": {
"ID": "IN",
"LocalizedName": "India",
"EnglishName": "India"
},
"AdministrativeArea": {
"ID": "WB",
"LocalizedName": "West Bengal",
"EnglishName": "West Bengal",
"Level": 1,
"LocalizedType": "State",
"EnglishType": "State",
"CountryID": "IN"
},
"TimeZone": {
"Code": "IST",
"Name": "Asia/Kolkata",
"GmtOffset": 5.5,
"IsDaylightSaving": false,
"NextOffsetChange": null
},
"GeoPosition": {
"Latitude": 22.075,
"Longitude": 87.6,
"Elevation": {
"Metric": {
"Value": 8,
"Unit": "m",
"UnitType": 5
},
"Imperial": {
"Value": 26,
"Unit": "ft",
"UnitType": 0
}
}
},
"IsAlias": false,
"SupplementalAdminAreas": [
{
"Level": 2,
"LocalizedName": "Purba Medinipur",
"EnglishName": "Purba Medinipur"
},
{
"Level": 3,
"LocalizedName": "Potashpur - I",
"EnglishName": "Potashpur - I"
}
],
"DataSets": [
"AirQuality",
"PremiumAirQuality"
]
},
{
"Version": 1,
"Key": "3190635",
"Type": "City",
"Rank": 85,
"LocalizedName": "Agarpara",
"EnglishName": "Agarpara",
"PrimaryPostalCode": "",
"Region": {
"ID": "ASI",
"LocalizedName": "Asia",
"EnglishName": "Asia"
},
"Country": {
"ID": "IN",
"LocalizedName": "India",
"EnglishName": "India"
},
"AdministrativeArea": {
"ID": "WB",
"LocalizedName": "West Bengal",
"EnglishName": "West Bengal",
"Level": 1,
"LocalizedType": "State",
"EnglishType": "State",
"CountryID": "IN"
},
"TimeZone": {
"Code": "IST",
"Name": "Asia/Kolkata",
"GmtOffset": 5.5,
"IsDaylightSaving": false,
"NextOffsetChange": null
},
"GeoPosition": {
"Latitude": 23.005,
"Longitude": 87.762,
"Elevation": {
"Metric": {
"Value": 29,
"Unit": "m",
"UnitType": 5
},
"Imperial": {
"Value": 95,
"Unit": "ft",
"UnitType": 0
}
}
},
"IsAlias": false,
"SupplementalAdminAreas": [
{
"Level": 2,
"LocalizedName": "Barddhaman",
"EnglishName": "Barddhaman"
},
{
"Level": 3,
"LocalizedName": "Raina - II",
"EnglishName": "Raina - II"
}
],
"DataSets": [
"AirQuality",
"PremiumAirQuality"
]
},
{
"Version": 1,
"Key": "672024",
"Type": "City",
"Rank": 600,
"LocalizedName": "Agarpara",
"EnglishName": "Agarpara",
"PrimaryPostalCode": "",
"Region": {
"ID": "ASI",
"LocalizedName": "Asia",
"EnglishName": "Asia"
},
"Country": {
"ID": "BD",
"LocalizedName": "Bangladesh",
"EnglishName": "Bangladesh"
},
"AdministrativeArea": {
"ID": "C",
"LocalizedName": "Dhaka",
"EnglishName": "Dhaka",
"Level": 1,
"LocalizedType": "Division",
"EnglishType": "Division",
"CountryID": "BD"
},
"TimeZone": {
"Code": "BDT",
"Name": "Asia/Dhaka",
"GmtOffset": 6,
"IsDaylightSaving": false,
"NextOffsetChange": null
},
"GeoPosition": {
"Latitude": 23.767,
"Longitude": 90.65,
"Elevation": {
"Metric": {
"Value": 4,
"Unit": "m",
"UnitType": 5
},
"Imperial": {
"Value": 13,
"Unit": "ft",
"UnitType": 0
}
}
},
"IsAlias": false,
"SupplementalAdminAreas": [],
"DataSets": []
}
]
I want to retrieve the key value ..so what should be the accurate code to get the value of key
Upvotes: 1
Views: 680
Reputation: 4220
Try this
try
{
JSONArray jsonArray = new JSONArray(s);
Weather wthr = new Weather();
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String key = jsonObject.getString("Key");
wthr.setKey(key);
Toast.makeText(getApplicationContext(), key, Toast.LENGTH_SHORT).show();
String country = jsonObject.getJSONObject("Country").getString("EnglishName");
wthr.setCountry(country);
Toast.makeText(getApplicationContext(), country, Toast.LENGTH_SHORT).show();
Log.i("MainActivity", "Key:" + key);
Log.i("MainActivity", "Country:" + country);
}
} catch (JSONException e) {
e.printStackTrace();
}
Upvotes: 0
Reputation: 6961
Keep in mind when parsing JSON response that everything enclosed in square brackets [] is a JSONArray and everything enclosed in curly brackets {} is a JSONObject.
Apply this logic to JSON of any difficulty and you'll have no problem parsing it.
Upvotes: 1
Reputation: 2962
You need something like this..
JSONArray jsonarray = new JSONArray(s);
for (int i = 0; i < jsonarray.length(); i++) {
JSONObject jsonobject = jsonarray.getJSONObject(i);
Weather wthr=new Weather();
String key=jsonobject .getString("Key");
wthr.setKey(key);
Toast.makeText(getApplicationContext(),key,Toast.LENGTH_SHORT).show();
String country=jsonobject .getJSONObject("Country").getString("EnglishName");
wthr.setCountry(country);
}
Upvotes: 1