如何在android中使用两个数组处理json数据?

时间:2014-05-18 11:39:01

标签: android arrays json object

我想从远程网址

获取两个json数组

我正在使用AsyncTask来做到这一点,但我无法获得任何数据!

@Override
        protected Void doInBackground(String... params) {

            try {
                // Creating JSON Parser instance
                JSONParser jParser = new JSONParser();

                // getting JSON string from URL
                String json = jParser.getJSONFromUrl(params[0]);

                // Getting Array of Contacts
                data = new JSONArray(json);

                JSONArray cities = data.getJSONArray();
                // looping through All cities
                for (int i = 0; i < cities.length(); i++) {

                    JSONObject e = cities.getJSONObject(i);

                    String ci_name = e.getString("ct_name");
                    String ci_web_id = e.getString("ct_id");

                    db.addCity(ci_name, ci_web_id);

                    db.closeDatabase();
                }

                JSONArray districts = data.getJSONArray(1);
                // looping through All districts
                for (int i = 0; i < districts.length(); i++) {

                    JSONObject e = districts.getJSONObject(i);

                    String di_name = e.getString("ar_name");
                    String di_web_id = e.getString("ar_id");

                    db.addDistrict(di_name, di_web_id);

                    db.closeDatabase();
                }

            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;
        }

返回数据是这样的:

{"city":[
{"ct_id":"1432","ct_name":"\u062e\u0645\u064a\u0633 \u0645\u0634\u064a\u0637","ct_hide":"0","ct_ord":"0","ct_created":"0"},
{"ct_id":"1434","ct_name":"\u0639\u0633\u064a\u0631","ct_hide":"0","ct_ord":"0","ct_created":"0"},{"ct_id":"1435","ct_name":"\u0627\u0644\u0645\u0646\u0637\u0642\u0629 \u0627\u0644\u0634\u0631\u0642\u064a\u0629","ct_hide":"0","ct_ord":"0","ct_created":"0"}
], "area":[
    {"ar_id":"1422","ar_name":"\u0627\u0644\u0645\u062f\u064a\u0646\u0629 \u0627\u0644\u0645\u0646\u0648\u0631\u0647","ar_hide":null,"ar_ord":null,"ar_created":null},    {"ar_id":"1433","ar_name":"\u0646\u062c\u0631\u0627\u0646","ar_hide":null,"ar_ord":null,"ar_created":null}]
}

2 个答案:

答案 0 :(得分:0)

您的json是JSONObject而不是JSONarray

 data = new JSONArray(json);

错了。

{  // json object node 
    "city": [  // json array city
        {      // json object
            "ct_id": "1432",
            "ct_name": "خميس مشيط",
            "ct_hide": "0",
            "ct_ord": "0",
            "ct_created": "0"
        },
        {
            "ct_id": "1434",
            "ct_name": "عسير",
            "ct_hide": "0",
            "ct_ord": "0",
            "ct_created": "0"
        },
        {
            "ct_id": "1435",
            "ct_name": "المنطقة الشرقية",
            "ct_hide": "0",
            "ct_ord": "0",
            "ct_created": "0"
        }
    ],
    "area": [  // json array area
        {
            "ar_id": "1422",
            "ar_name": "المدينة المنوره",
            "ar_hide": null,
            "ar_ord": null,
            "ar_created": null
        },
        {
            "ar_id": "1433",
            "ar_name": "نجران",
            "ar_hide": null,
            "ar_ord": null,
            "ar_created": null
        }
    ]
}

解析

JSONObject jb = new JSONObject(json);
        JSONArray city = jb.getJSONArray("city");
        for(int i=0;i<city.length();i++)
        {
            JSONObject jb1 = city.getJSONObject(i);
            String id = jb1.getString("ct_id");
            String name = jb1.getString("ct_name");
            String hide = jb1.getString("ct_hide");
            String ord = jb1.getString("ct_ord");
            String created = jb1.getString("ct_ord");
            Log.i("city id is",id);

        }
        JSONArray area = jb.getJSONArray("area");
        for(int i=0;i<area.length();i++)
        {
            JSONObject jb1 = area.getJSONObject(i);
            String id = jb1.getString("ar_id");
            String name = jb1.getString("ar_name");
            String hide = jb1.getString("ar_hide");
            String ord = jb1.getString("ar_ord");
            String created = jb1.getString("ar_ord");
            Log.i("Area id is",id);

        }

您还可以考虑使用gson将json解析为java对象

http://code.google.com/p/google-gson/

答案 1 :(得分:0)

我没有看到任何远程网址请求。你如何从服务器获取数据?

通常,它看起来像这样:

public void execute() {
    final AndroidHttpClient client = AndroidHttpClient.newInstance("TAG");
    try {
        HttpUriRequest request = getRequest();
        HttpResponse response = client.execute(request);
        final int code = response.getStatusLine().getStatusCode();
        Log.d("TAG", "Server returns " + code);
        if (code == HttpStatus.SC_OK) {
            String json = EntityUtils.toString(response.getEntity());
            handleResult(json);
        }
    } catch (IOException e) {
        Log.e("TAG", "Failed to execute response", e);
    }
}
private void handleResult(String json) {
    try {
        JSONObject jObject = new JSONObject(json);//your response is not an array
        JSONArray content = jObject.getJSONArray("city")
        final int count = content.length();
        for (int i = 0; i < count; i++) {
            JSONObject city = content.getJSONObject(i);
            Log.d("TAG", city.getString("ct_id"));
        }

    } catch (JSONException e) {
        Log.e("TAG", "Failed to obtain json", e);
    }
}