如何从android中的json问题访问值

时间:2015-04-14 08:00:04

标签: android json

我正在努力获取“鳕鱼”的价值。来自json字符串:

 {"coord":{"lon":73.86,"lat":18.52},"sys":{"message":0.0293,"country":"IN","sunrise":1428972502,"sunset":1429017681},"weather":[{"id":801,"main":"Clouds","description":"few clouds","icon":"02d"}],"base":"stations","main":{"temp":304.301,"temp_min":304.301,"temp_max":304.301,"pressure":951.61,"sea_level":1021.56,"grnd_level":951.61,"humidity":36},"wind":{"speed":2.06,"deg":302.501},
        "clouds":{"all":24},"dt":1428996633,"id":1259229,"name":"Pune","cod":200}

但我无法从我的代码中获取此值。我用来从json字符串访问此值的代码如下:

  try{
                   JSONObject jsObject=(new JSONObject(JsonString)).getJSONObject("coord");

                    if(  jsObject.getInt("cod")==200) {

                        i.putExtra("jsn", JsonString);
                        i.putExtra("city", etCity.getText().toString());
                        startActivity(i);
                    }

4 个答案:

答案 0 :(得分:2)

在if条件下,您尝试访问数组"cod"中的密钥{"lon":73.86,"lat":18.52}。它将抛出JSONException

试试这个:

try{
    JSONObject jsonmain = new JSONOBject(JsonString);
    if(jsonmain.getInt("cod") == 200) {
        i.putExtra("jsn", JsonString);
        i.putExtra("city", etCity.getText().toString());
        startActivity(i);
}

答案 1 :(得分:1)

你的json是:

{
"coord": {
    "lon": 73.86,
    "lat": 18.52
},
"sys": {
    "message": 0.0293,
    "country": "IN",
    "sunrise": 1428972502,
    "sunset": 1429017681
},
"weather": [
    {
        "id": 801,
        "main": "Clouds",
        "description": "few clouds",
        "icon": "02d"
    }
],
"base": "stations",
"main": {
    "temp": 304.301,
    "temp_min": 304.301,
    "temp_max": 304.301,
    "pressure": 951.61,
    "sea_level": 1021.56,
    "grnd_level": 951.61,
    "humidity": 36
},
"wind": {
    "speed": 2.06,
    "deg": 302.501
},
"clouds": {
    "all": 24
},
"dt": 1428996633,
"id": 1259229,
"name": "Pune",
"cod": 200
}

密钥" cod"并没有嵌套在" coord"。

尝试:     new JSONObject(JsonString)).getInt("cod");

答案 2 :(得分:0)

URL url = new URL(String.format(//url goes here));           
            HttpURLConnection connection = 
                    (HttpURLConnection)url.openConnection();

            connection.addRequestProperty("x-api-key", 
                    context.getString(R.string.open_weather_maps_app_id));

            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(connection.getInputStream()));

            StringBuffer json = new StringBuffer(1024);
            String tmp="";
            while((tmp=reader.readLine())!=null)
                json.append(tmp).append("\n");
            reader.close();

            JSONObject data = new JSONObject(json.toString());

            // This value will be 404 if the request was not
            // successful
            if(data.getInt("cod") != 200){
                return null;
            }

您关注Create a Weather App on Android

答案 3 :(得分:0)

实际上你正在尝试错误的JSON对象。

  String cod=(new JSONObject(JsonString)).getJSONObject("code").toString();
                 //you may need to try catch block
                if(  Integer.parseint(cod)==200) {

                   .... Your logic
                }