【问题标题】:How to get handle json data with two arrays in android?如何在android中使用两个数组处理json数据?
【发布时间】:2014-05-18 11:39:00
【问题描述】:

我想从远程url获取两个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}]
}

【问题讨论】:

    标签: android arrays json object


    【解决方案1】:

    您的 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/

    【讨论】:

      【解决方案2】:

      我没有看到对远程 url 的任何请求。您如何从服务器获取数据?

      一般情况下是这样的:

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

      【讨论】:

      • 这是 op 从字符串 json = jParser.getJSONFromUrl(params[0]); 获取 json 的地方。 op 尚未发布JSONParser.java
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-19
      • 2015-08-13
      • 1970-01-01
      相关资源
      最近更新 更多