如何获取没有数组名称的json数组?

时间:2015-03-26 10:06:51

标签: android json

我试图通过JSON从服务器获取数据,但它没有数组名称。我没有使用过这种类型的JSON,请帮助我获取数据。

这是我的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("properties");
                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"));

                     JSONObject links = object.getJSONObject("meta").getJSONObject("links");
                     String collectionLink = links.getString("collection");**/

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



                /** 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 :(得分:0)

鉴于返回的JSON的结构,我想您可能想尝试将外部对象创建为JSONArray而不是JSONObject。

所以摆脱&#34; jsono&#34;变量,并改为此行

JSONArray jarray = jsono.getJSONArray(data);