-1

我在解析 Facebook JSon 数据时遇到问题,我的数据格式是这样的。我在谷歌上搜索了很多时间,但没有得到价值。请建议我如何解决这个问题。您的宝贵答案将不胜感激。

json

{
   "id": "",
   "name": "",
   "first_name": "",
   "middle_name": "",
   "last_name": "",
   "link": "",
   "username": "",
   "birthday": "",
   "location": {
      "id": "",
      "name": ", "
   }

更新 Actullay 我有 https://graph.facebook.com/me?access_token= 获取 session.getAccessToken() 并使其完整 url URL_PREFIX_FRIENDS + session.getAccessToken() 然后发送 url 用于 http 连接并获取 Jsonobject 问题是这个而不是jSonObject 它给出了真正的结果(不知道为什么以及如何)

httpConnection 的代码

public class JsonParsing {

    static InputStream is = null;
    static JSONObject jObj = null;
    static String json = "";

    // constructor
    public JsonParsing() {

    }

    public JSONObject getJSONFromUrl(String url) {

        // Making HTTP request
        try {
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);
            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

            StringBuilder buffer = new StringBuilder();

    BufferedReader reader = new BufferedReader(new InputStreamReader(
            in, HTTP.UTF_8));

    String line = null;
    try {
        while ((line = reader.readLine()) != null) {
            buffer.append(line);
        }
            json = buffer.toString();

    } finally {
        in.close();
        reader.close();
    }



        // try parse the string to a JSON object
        try {
            jObj = new JSONObject(json);
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        // return JSON String
        return jObj;

    }
}

之后将其发送到解析器并出现以下错误

日志猫

E/JSON Parser( 5660): Error parsing data org.json.JSONException: Value true of type java.lang.Boolean cannot be converted to JSONObject
4

3 回答 3

1

使用以下代码。

 JSONObject json=new JSONObject(result);
      String detail=json.getString("id");
      String image=json.getString("name");  
于 2013-11-11T06:08:08.257 回答
0

更新:用这个替换你的函数

public JSONObject getJSONFromUrl(String url) {

        // Making HTTP request
        InputStream is = null;
        JSONObject jobj = null;
        String json = null;
        try {
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpGet httpPost = new HttpGet(url);
            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();


            StringBuilder buffer = new StringBuilder();

            BufferedReader reader = new BufferedReader(new InputStreamReader(is, HTTP.UTF_8));

            String line = null;
            try {
                while ((line = reader.readLine()) != null) {
                    buffer.append(line);
                }
                json = buffer.toString();

            } finally {
                is.close();
                reader.close();
            }

            try {
                jobj = new JSONObject(json);
                System.out.println("JSON : " + jobj);
            } catch (JSONException e) {
                Log.e("JSON Parser", "Error parsing data " + e.toString());
            }

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        // try parse the string to a JSON object

        // return JSON String
        return jobj;

    }

试试这个

此函数将递归迭代您的 json。任何 json 都会动态解析。

private void parseJson(JSONObject data) {

        if (data != null) {
            Iterator<String> it = data.keys();
            while (it.hasNext()) {
                String key = it.next();
                try {
                    if (data.get(key) instanceof JSONArray) {
                        JSONArray arry = data.getJSONArray(key);
                        int size = arry.length();
                        for (int i = 0; i < size; i++) {
                            parseJson(arry.getJSONObject(i));
                        }
                    } else if (data.get(key) instanceof JSONObject) {
                        parseJson(data.getJSONObject(key));
                    } else {
                        System.out.println("Key :" + key);
                        System.out.println("Value :" + data.getString(key));
                    }
                } catch (Throwable e) {
                    try {
                        System.out.println("Key :" + key);
                        System.out.println("Value :" + data.getString(key));
                    } catch (Exception ee) {
                    }
                    e.printStackTrace();

                }
            }
        }
    }

调用此函数并查看您的 logcat。希望这对您有所帮助。

于 2013-11-11T05:58:55.627 回答
0

将 Facebook Response 设为 agraphObject并获取其键的值。

GraphObject facebookResponseGraphObject = facebookResponse.getGraphObject();
JSONObject facebookResponseJSONObject = facebookResponseGraphObject.getInnerJSONObject();

String id = json_obj.getString("id");
String name = json_obj.getString("name");
.
.
.

facebookResponse是你得到的回应onCompleted()

于 2013-11-11T06:09:40.393 回答