android JSON解析データ-解析天気予報

6302 ワード

#概要筆者は最近、天気予報JSONデータの解析を行いました.天気予報インタフェース:http://wthrcdn.etouch.cn/weather_mini?citykey=101200101JSONデータは以下の通りです.
{
    "desc": "OK",
    "status": 1000,
    "data": {
        "wendu": "14",
        "ganmao": "    ,      ,      ,              。",
        "forecast": [
            {
                "fengxiang": "     ",
                "fengli": "   ",
                "high": "   17℃",
                "type": "  ",
                "low": "   10℃",
                "date": "30    "
            },
            {
                "fengxiang": "     ",
                "fengli": "   ",
                "high": "   18℃",
                "type": "  ",
                "low": "   7℃",
                "date": "31    "
            },
            {
                "fengxiang": "     ",
                "fengli": "   ",
                "high": "   20℃",
                "type": " ",
                "low": "   8℃",
                "date": "1    "
            },
            {
                "fengxiang": "     ",
                "fengli": "   ",
                "high": "   23℃",
                "type": " ",
                "low": "   10℃",
                "date": "2    "
            },
            {
                "fengxiang": "     ",
                "fengli": "   ",
                "high": "   23℃",
                "type": "  ",
                "low": "   12℃",
                "date": "3    "
            }
        ],
        "yesterday": {
            "fl": "  ",
            "fx": "     ",
            "high": "   21℃",
            "type": " ",
            "low": "   12℃",
            "date": "29    "
        },
        "aqi": "114",
        "city": "  "
    }
}

最終解析効果:android JSON解析数据-解析天气预报_第1张图片
#解析概要1、まず、受け取ったデータ全体をJSOnObjectオブジェクトに変換できます.2、データ全体のJSOnObjectオブジェクトからdataのデータを取得し、JSOnObjectオブジェクトでもある.dataでは,このときの温度や都市などの情報が得られる.3、dataのJSOnObjectオブジェクトでforecastのデータを取得でき、forecastのデータはJSOnArrayオブジェクトである.4、forecastのJSOnArrayオブジェクトでは、ここ数日の天気情報を取得できます.
#コードの便宜上、筆者はvolleyフレームワークを使用し、読者が新しいプロジェクトをbuildで作成する必要がある.gradleのdependenciesには、次のように追加されます.
compile 'eu.the4thfloor.volley:com.android.volley:2015.05.28'

MainActivity.java:
package com.example.double2.jsontext;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.TextView;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class MainActivity extends AppCompatActivity {

    private TextView tvMain;
    private RequestQueue mRequestQueue;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
    }

    private void initView() {
        tvMain = (TextView) findViewById(R.id.tv_main);
        mRequestQueue = Volley.newRequestQueue(this);

        JsonObjectRequest mJsonObjectRequest = new JsonObjectRequest(
                "http://wthrcdn.etouch.cn/weather_mini?citykey=101200101",
                null,
                new Response.Listener() {
                    @Override
                    public void onResponse(JSONObject response) {
                        try {
                            JSONObject data = new JSONObject(response.getString("data"));
                            JSONArray forecast = data.getJSONArray("forecast");
                            JSONObject todayWeather = forecast.getJSONObject(0);

                            String wendu = data.getString("wendu") + "
"; String ganmao = data.getString("ganmao") + "
"; String high = todayWeather.getString("high") + "
"; String low = todayWeather.getString("low") + "
"; String date = todayWeather.getString("date") + "
"; String city = data.getString("city") + "
"; tvMain.setText(wendu + ganmao + high + low + date+city); } catch (JSONException e) { e.printStackTrace(); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Log.e("TAG", error.getMessage(), error); } }); mRequestQueue.add(mJsonObjectRequest); } }

activity_main.xml: