如何解析单个JSON对象响应?

时间:2017-12-19 08:27:57

标签: android json api

我正在尝试实施城市搜索API并正确获取JSON响应,但无法解析JSON

我有日志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

我解析JSON的代码是 - &gt;

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

我应该如何解析这种类型的JSON?只有一个对象,没有名称。如何解析这个JSON? 以下是我从网址获得的完整JSON响应。

[
  {
    "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": []
  }
]

我想检索键值..那么获取键值

的准确代码应该是什么

3 个答案:

答案 0 :(得分:1)

在解析JSON响应时请记住,方括号[] 中包含的所有内容都是 JSONArray ,所有内容都包含在花括号{} 中是 JSONObject

将此逻辑应用于任何难度的JSON,您将解析它没有问题。

答案 1 :(得分:0)

你需要这样的东西..

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

答案 2 :(得分:0)

试试这个

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