无法从json检索数据

时间:2015-03-26 06:39:21

标签: arrays json

[  
   {  
      "ID":264,
      "title":"8BDR \u2013 villa",
      "status":"publish",
      "type":"properties",
      "author":{  
         "ID":1,
         "username":"bm",
         "name":"bm",
         "first_name":"",
         "last_name":"",
         "nickname":"bm",
         "slug":"bm",
         "URL":"",
         "avatar":"http:\/\/0.gravatar.com\/avatar\/81ff187fd3ab62858b098258fb3f0479?s=96",
         "description":"",
         "registered":"2015-01-11T07:25:06+00:00",
         "meta":{  
            "links":{  
               "self":"http:\/\/titusandbindu.com\/bm\/wp-json\/users\/1",
               "archives":"http:\/\/titusandbindu.com\/bm\/wp-json\/users\/1\/posts"
            }
         }
      },
      "content":"",
      "parent":0,
      "link":"http:\/\/titusandbindu.com\/bm\/blog\/properties\/8bdr-villa\/",
      "date":"2015-03-18T08:32:24+00:00",
      "modified":"2015-03-18T08:52:04+00:00",
      "format":"standard",
      "slug":"8bdr-villa",
      "guid":"http:\/\/titusandbindu.com\/bm\/?post_type=properties&p=264",
      "excerpt":null,
      "menu_order":0,
      "comment_status":"closed",
      "ping_status":"closed",
      "sticky":false,
      "date_tz":"UTC",
      "date_gmt":"2015-03-18T08:32:24+00:00",
      "modified_tz":"UTC",
      "modified_gmt":"2015-03-18T08:52:04+00:00",
      "meta":{  
         "links":{  
            "self":"http:\/\/titusandbindu.com\/bm\/wp-json\/posts\/264",
            "author":"http:\/\/titusandbindu.com\/bm\/wp-json\/users\/1",
            "collection":"http:\/\/titusandbindu.com\/bm\/wp-json\/posts",
            "replies":"http:\/\/titusandbindu.com\/bm\/wp-json\/posts\/264\/comments",
            "version-history":"http:\/\/titusandbindu.com\/bm\/wp-json\/posts\/264\/revisions"
         }
      },
      "_edit_lock":[  
         "1426668679:1"
      ],
      "_edit_last":[  
         "1"
      ],
      "_wp_trash_meta_status":[  
         "draft"
      ],
      "_wp_trash_meta_time":[  
         "1426668336"
      ],
      "_thumbnail_id":[  
         "267"
      ],
      "bm_property_owner_name":[  
         " AMMAR SAMI"
      ],
      "bm_property_owner_phone":[  
         "66610001"
      ],
      "bm_property_owner_email":[  
         "a.s.qambar@gmail.com"
      ],
      "bm_property_owner_address":[  
         "HIDD"
      ],
      "bm_property_category":[  
         "Villa"
      ],
      "bm_property_transaction_type":[  
         "BUY"
      ],
      "bm_property_price":[  
         "300,000"
      ],
      "bm_property_location":[  
         "HIDD"
      ],
      "bm_property_area":[  
         "350 Sqm"
      ],
      "bm_property_no_rooms":[  
         "8"
      ],
      "offer_shop_details":false,
      "featured_image":{  
         "ID":267,
         "title":"IMG_6948",
         "status":"inherit",
         "type":"attachment",
         "author":{  
            "ID":1,
            "username":"bm",
            "name":"bm",
            "first_name":"",
            "last_name":"",
            "nickname":"bm",
            "slug":"bm",
            "URL":"",
            "avatar":"http:\/\/0.gravatar.com\/avatar\/81ff187fd3ab62858b098258fb3f0479?s=96",
            "description":"",
            "registered":"2015-01-11T07:25:06+00:00",
            "meta":{  
               "links":{  
                  "self":"http:\/\/titusandbindu.com\/bm\/wp-json\/users\/1",
                  "archives":"http:\/\/titusandbindu.com\/bm\/wp-json\/users\/1\/posts"
               }
            }
         },
         "content":"<p class=\"attachment\"><a href='http:\/\/titusandbindu.com\/bm\/wp-content\/uploads\/2015\/03\/IMG_6948.jpg'><img width=\"250\" height=\"167\" src=\"http:\/\/titusandbindu.com\/bm\/wp-content\/uploads\/2015\/03\/IMG_6948-250x167.jpg\" class=\"attachment-medium\" alt=\"IMG_6948\" \/><\/a><\/p>\n",
         "parent":264,
         "link":"http:\/\/titusandbindu.com\/bm\/blog\/properties\/8bdr-villa\/img_6948\/",
         "date":"2015-03-18T08:51:18+00:00",
         "modified":"2015-03-18T08:51:18+00:00",
         "format":"standard",
         "slug":"img_6948",
         "guid":"http:\/\/titusandbindu.com\/bm\/wp-content\/uploads\/2015\/03\/IMG_6948.jpg",
         "excerpt":null,
         "menu_order":0,
         "comment_status":"open",
         "ping_status":"open",
         "sticky":false,
         "date_tz":"UTC",
         "date_gmt":"2015-03-18T08:51:18+00:00",
         "modified_tz":"UTC",
         "modified_gmt":"2015-03-18T08:51:18+00:00",
         "meta":{  
            "links":{  
               "self":"http:\/\/titusandbindu.com\/bm\/wp-json\/media\/267",
               "author":"http:\/\/titusandbindu.com\/bm\/wp-json\/users\/1",
               "collection":"http:\/\/titusandbindu.com\/bm\/wp-json\/media",
               "replies":"http:\/\/titusandbindu.com\/bm\/wp-json\/media\/267\/comments",
               "version-history":"http:\/\/titusandbindu.com\/bm\/wp-json\/media\/267\/revisions",
               "up":"http:\/\/titusandbindu.com\/bm\/wp-json\/media\/264"
            }
         },
         "_wp_attached_file":[  
            "2015\/03\/IMG_6948.jpg"
         ],
         "_wp_attachment_metadata":[  
            "a:5:{s:5:\"width\";i:1280;s:6:\"height\";i:853;s:4:\"file\";s:20:\"2015\/03\/IMG_6948.jpg\";s:5:\"sizes\";a:8:{s:9:\"thumbnail\";a:4:{s:4:\"file\";s:20:\"IMG_6948-150x150.jpg\";s:5:\"width\";i:150;s:6:\"height\";i:150;s:9:\"mime-type\";s:10:\"image\/jpeg\";}s:6:\"medium\";a:4:{s:4:\"file\";s:20:\"IMG_6948-250x167.jpg\";s:5:\"width\";i:250;s:6:\"height\";i:167;s:9:\"mime-type\";s:10:\"image\/jpeg\";}s:5:\"large\";a:4:{s:4:\"file\";s:20:\"IMG_6948-700x466.jpg\";s:5:\"width\";i:700;s:6:\"height\";i:466;s:9:\"mime-type\";s:10:\"image\/jpeg\";}s:5:\"small\";a:4:{s:4:\"file\";s:19:\"IMG_6948-120x80.jpg\";s:5:\"width\";i:120;s:6:\"height\";i:80;s:9:\"mime-type\";s:10:\"image\/jpeg\";}s:11:\"custom-size\";a:4:{s:4:\"file\";s:21:\"IMG_6948-1100x245.jpg\";s:5:\"width\";i:1100;s:6:\"height\";i:245;s:9:\"mime-type\";s:10:\"image\/jpeg\";}s:14:\"shop_thumbnail\";a:4:{s:4:\"file\";s:20:\"IMG_6948-180x180.jpg\";s:5:\"width\";i:180;s:6:\"height\";i:180;s:9:\"mime-type\";s:10:\"image\/jpeg\";}s:12:\"shop_catalog\";a:4:{s:4:\"file\";s:20:\"IMG_6948-300x300.jpg\";s:5:\"width\";i:300;s:6:\"height\";i:300;s:9:\"mime-type\";s:10:\"image\/jpeg\";}s:11:\"shop_single\";a:4:{s:4:\"file\";s:20:\"IMG_6948-600x600.jpg\";s:5:\"width\";i:600;s:6:\"height\";i:600;s:9:\"mime-type\";s:10:\"image\/jpeg\";}}s:10:\"image_meta\";a:11:{s:8:\"aperture\";i:0;s:6:\"credit\";s:0:\"\";s:6:\"camera\";s:0:\"\";s:7:\"caption\";s:0:\"\";s:17:\"created_timestamp\";i:0;s:9:\"copyright\";s:0:\"\";s:12:\"focal_length\";i:0;s:3:\"iso\";i:0;s:13:\"shutter_speed\";i:0;s:5:\"title\";s:0:\"\";s:11:\"orientation\";i:1;}}"
         ],
         "offer_shop_details":false,
         "terms":[  

         ],
         "source":"http:\/\/titusandbindu.com\/bm\/wp-content\/uploads\/2015\/03\/IMG_6948.jpg",
         "is_image":true,
         "attachment_meta":{  
            "width":1280,
            "height":853,
            "file":"2015\/03\/IMG_6948.jpg",
            "sizes":{  
               "thumbnail":{  
                  "file":"IMG_6948-150x150.jpg",
                  "width":150,
                  "height":150,
                  "mime-type":"image\/jpeg",
                  "url":"http:\/\/titusandbindu.com\/bm\/wp-content\/uploads\/2015\/03\/IMG_6948-150x150.jpg"
               },
               "medium":{  
                  "file":"IMG_6948-250x167.jpg",
                  "width":250,
                  "height":167,
                  "mime-type":"image\/jpeg",
                  "url":"http:\/\/titusandbindu.com\/bm\/wp-content\/uploads\/2015\/03\/IMG_6948-250x167.jpg"
               },
               "large":{  
                  "file":"IMG_6948-700x466.jpg",
                  "width":700,
                  "height":466,
                  "mime-type":"image\/jpeg",
                  "url":"http:\/\/titusandbindu.com\/bm\/wp-content\/uploads\/2015\/03\/IMG_6948-700x466.jpg"
               },
               "small":{  
                  "file":"IMG_6948-120x80.jpg",
                  "width":120,
                  "height":80,
                  "mime-type":"image\/jpeg",
                  "url":"http:\/\/titusandbindu.com\/bm\/wp-content\/uploads\/2015\/03\/IMG_6948-120x80.jpg"
               },
               "custom-size":{  
                  "file":"IMG_6948-1100x245.jpg",
                  "width":1100,
                  "height":245,
                  "mime-type":"image\/jpeg",
                  "url":"http:\/\/titusandbindu.com\/bm\/wp-content\/uploads\/2015\/03\/IMG_6948-1100x245.jpg"
               },
               "shop_thumbnail":{  
                  "file":"IMG_6948-180x180.jpg",
                  "width":180,
                  "height":180,
                  "mime-type":"image\/jpeg",
                  "url":"http:\/\/titusandbindu.com\/bm\/wp-content\/uploads\/2015\/03\/IMG_6948-180x180.jpg"
               },
               "shop_catalog":{  
                  "file":"IMG_6948-300x300.jpg",
                  "width":300,
                  "height":300,
                  "mime-type":"image\/jpeg",
                  "url":"http:\/\/titusandbindu.com\/bm\/wp-content\/uploads\/2015\/03\/IMG_6948-300x300.jpg"
               },
               "shop_single":{  
                  "file":"IMG_6948-600x600.jpg",
                  "width":600,
                  "height":600,
                  "mime-type":"image\/jpeg",
                  "url":"http:\/\/titusandbindu.com\/bm\/wp-content\/uploads\/2015\/03\/IMG_6948-600x600.jpg"
               }
            },
            "image_meta":{  
               "aperture":0,
               "credit":"",
               "camera":"",
               "caption":"",
               "created_timestamp":0,
               "copyright":"",
               "focal_length":0,
               "iso":0,
               "shutter_speed":0,
               "title":"",
               "orientation":1
            }
         }
      },
      "terms":[  

      ]
   },

这是我的数组[0],json和我一样有数组[1],数组[3]。他们已经将url直接指向json没有指定oblect的数组,或者没有给出键值。如何通过以下方式访问数据是我的java代码。

class JSONAsyncTask extends AsyncTask<String, Void, Boolean> {

    ProgressDialog dialog;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        dialog = new ProgressDialog(Properties.this);
        dialog.setMessage("Loading, please wait");
        dialog.setTitle("Connecting server");
        dialog.show();
        dialog.setCancelable(false);
    }

    @Override
    protected Boolean doInBackground(String... urls) {
        try {

            //------------------>>
            HttpGet httppost = new HttpGet(urls[0]);
            HttpClient httpclient = new DefaultHttpClient();
            HttpResponse response = httpclient.execute(httppost);

            // StatusLine stat = response.getStatusLine();
            int status = response.getStatusLine().getStatusCode();

            if (status == 200) {
                HttpEntity entity = response.getEntity();
                String data = EntityUtils.toString(entity);


                JSONObject jsono = new JSONObject(data);
                JSONArray jarray = jsono.getJSONArray("i");
                //int length = jarray.length();
                //System.out.println("length" + length);

                for (int i = 0; i < jarray.length(); i++) {
                    JSONObject object = jarray.getJSONObject(i);

                    Properties_Property actor = new Properties_Property();

                     JSONObject author = object.getJSONObject("author");
                     String authorName = author.getString("name");

                     actor.setTitle(object.getString("name"));





                    //actor.getTitle(object.getString("_wp_trash_meta_status"));
                /** actor.setHeight(object.getString("height"));
                    actor.setDob(object.getString("dob"));
                    actor.setCountry(object.getString("country"));
                    actor.setName(object.getString("image"));**/

                    propertyList.add(actor);
                }
                return true;
            }

            //------------------>>

        } catch (ParseException e1) {
            e1.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return false;
    }

    protected void onPostExecute(Boolean result) {
        dialog.cancel();
        adapter.notifyDataSetChanged();
        if(result == false)
            Toast.makeText(getApplicationContext(), "Unable to fetch data from server", Toast.LENGTH_LONG).show();

    }

1 个答案:

答案 0 :(得分:3)

您的JSON数据似乎是json数组格式。所以 改变

JSONObject jsono = new JSONObject(data);
JSONArray jarray = jsono.getJSONArray("i");

JSONArray jarray = new JSONArray(data);