如何从没有数组名称的android中的json获取数据?

时间:2014-06-05 09:46:30

标签: android json

我想从此链接获取网址:

 "http://graph.facebook.com/10202459285618351/picture?type=large&redirect=false"

它给出了结果:

{    “数据”:{       “url”:“https://fbcdn-profile-a.akamaihd.net/hprofile-ak-xap1/t1.0- 1 / s200x200 / 10342822_10202261537234765_3194866551853134720_n.jpg”,       “is_silhouette”:false    } }

我试过了,

private class GetContacts extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // Showing progress dialog
        pDialog = new ProgressDialog(AccountActivity.this);
        pDialog.setMessage("Please wait...");
        pDialog.setCancelable(false);
        pDialog.show();

    }

    @Override
    protected Void doInBackground(Void... arg0) {
        // Creating service handler class instance
        ServiceHandler sh = new ServiceHandler();

        String jsonStr = sh.makeServiceCall(fbPicURL, ServiceHandler.GET);

        Log.d("Response: ", "> " + jsonStr);

        if (jsonStr != null) {
            try {
                JSONObject jsonObj = new JSONObject(jsonStr);

                // Getting JSON Array node
                fbRealURL = jsonObj.getString("url");


                    // Phone node is JSON Object
                    Toast.makeText(AccountActivity.this, fbRealURL,
                            Toast.LENGTH_LONG).show();
                    // tmp hashmap for single contact

                }
             catch (JSONException e) {
                e.printStackTrace();
            }
        } else {
            Log.e("ServiceHandler", "Couldn't get any data from the url");
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        // Dismiss the progress dialog
        if (pDialog.isShowing())
            pDialog.dismiss();
        /**
         * Updating parsed JSON data into ListView
         * */
        Toast.makeText(AccountActivity.this, fbRealURL,
                Toast.LENGTH_LONG).show();
    }

}

但是返回null并且它没有崩溃..

1 个答案:

答案 0 :(得分:1)

您必须首先获取数据对象:

     try {
            JSONObject jsonObj = new JSONObject(jsonStr);


            fbRealURLObj = jsonObj.getJSONObject("data");

            fbRealURL = fbRealURLObj.getString("url");

                // Phone node is JSON Object
                Toast.makeText(AccountActivity.this, fbRealURL,
                        Toast.LENGTH_LONG).show();
                // tmp hashmap for single contact

            }