Android的 - 如何解析来自JSON阵列和显示吐司特定值 [英] Android - How to parse specific values from JSON Array and display Toast

查看:206
本文介绍了Android的 - 如何解析来自JSON阵列和显示吐司特定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

新到Android和Java的一般,我学习如何做一个JSON调用。要做到这一点,我是按照本指南:的http:// mobiforge。 COM /设计开发/消费JSON的服务,Android的应用

New to Android and Java in general and I'm learning how to make a JSON call. To do so, I'm following this guide: http://mobiforge.com/design-development/consuming-json-services-android-apps

这就是事情让我困惑。该教程的作者希望读者调用此API: HTTP://ws.geonames。组织/ findNearByWeatherJSON?纬度= 37lng = 122

Here's where things get confusing for me. The author of that tutorial wants the reader to call this API: http://ws.geonames.org/findNearByWeatherJSON?lat=37lng=122

在这种格式返回一个JSON对象:

Which returns a JSON object in this format:

            {
                "weatherObservation": {
                     "clouds":"scattered clouds",
                     "weatherCondition":"n/a",
                     "observation":"KCFV 090852Z AUTO 06005KT 
                      10SM SCT090 SCT110 24/20 A3000 RMK AO2 
                      SLP148 T02390200 53002",
                     "windDirection":60,
                     "ICAO":"KCFV",
                     "seaLevelPressure":1014.8,
                     "elevation":225,
                     "countryCode":"US",
                     "lng":-95.56666666666666,
                     "temperature":"23.9",
                     "dewPoint":"20",
                     "windSpeed":"05",
                     "humidity":78,
                     "stationName":"Coffeyville, Coffeyville 
                                 Municipal Airport",
                     "datetime":"2012-07-09 08:52:00",
                     "lat":37.083333333333336
                }
            }

pretty直线前进,除了API不再有效/有限制。为了完成这个项目我已经改为选择调用这个API:的 http://api.openweathermap.org/data/2.5/weather?lat=37.77&lon=-122.419

在这种格式返回的JSON

Which returns the JSON in this format

{
    "coord": {
        "lon": 139,
        "lat": 35
    },
    "sys": {
        "country": "JP",
        "sunrise": 1369769524,
        "sunset": 1369821049
    },
    "weather": [
        {
            "id": 804,
            "main": "clouds",
            "description": "overcast clouds",
            "icon": "04n"
        }
    ],
    "main": {
        "temp": 289.5,
        "humidity": 89,
        "pressure": 1013,
        "temp_min": 287.04,
        "temp_max": 292.04
    },
    "wind": {
        "speed": 7.31,
        "deg": 187.002
    },
    "rain": {
        "3h": 0
    },
    "clouds": {
        "all": 92
    },
    "dt": 1369824698,
    "id": 1851632,
    "name": "Shuzenji",
    "cod": 200
}

我可以拨打电话得很好,但我怎么了全天候的阵列中显示主和说明字符串?更具体地讲,我怎么显示该信息为吐司?

I can make the call just fine, but how do I display the "main" and "description" strings in the "weather" array? More specifically, how do I display this information as a Toast?

下面是我有:

protected void onPostExecute(String result){

        try {


            JSONArray weatherArray = new JSONArray(result);
            JSONArray wArray = new JSONArray("weather");

            String mainWeather = wArray.getString(1);
            String mainDescription = wArray.getString(2);

            Toast.makeText(getBaseContext(), mainWeather + " - "
                        + mainDescription,Toast.LENGTH_SHORT).show();

        } catch (Exception e) {
            Log.d("ReadWeatherJSONFeedTask", e.getLocalizedMessage());
        }

BecauseI我的mobiforge教程之后,我也没有偏离其他地方除了code这个特定块。

BecauseI am following the mobiforge Tutorial, I have not deviated anywhere else except for this particular block of code.

感谢您的帮助!

编辑:
有几种解决方案在这里,看到工作@swats和@ user3515851。我选择了@ remees-M-syde,由于它的简单性。这主要是因为他的解决方案并不需要我去通过循环。

There are several solutions here that work see @swats and @user3515851. I have chosen @remees-m-syde due to it's simplicity. Primarily because his solution did not require that I go through the for loop.

推荐答案

您无法获得数据,因为那里是全天候的JSONArray内的一个JSON对象。

You are unable to get the data because there is one json object inside the "weather" JSONArray.

JSONArray开头 - [

JSONArray starts with - [

JSONObject的开头 - {

JSONObject starts with - {,

因此​​,首先获得JSONArray,然后它里面的JSONObject。

So first get the JSONArray and then the JSONObject inside it.

"weather": [                          ----Array
        {                             ----Object
            "id": 804,
            "main": "clouds",
            "description": "overcast clouds",
            "icon": "04n"
        }
    ]

您必须得到这个JSONObject的,然后从中获取字符串,如低于code表现。

You have to get this JSONObject and then get the String from it like the below code showing.

    JSONObject weatherArray = new JSONObject(result);
    JSONArray wArray = weatherArray.getJSONArray("weather");
    JSONObject jobj = wArray.getJSONObject(0);
    String mainWeather = jobj.getString("main");
    String mainDescription = jobj.getString("description");
    Toast.makeText(getBaseContext(), mainWeather + " - "
                                + mainDescription,Toast.LENGTH_SHORT).show();

这篇关于Android的 - 如何解析来自JSON阵列和显示吐司特定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆