解析facebook JSON数据

时间:2013-11-11 05:47:43

标签: android json facebook

我有解析Facebook JSon数据的问题我的数据格式是这样的。我google了很多时间为此但没有得到价值。请建议我如何解决这个问题。你的宝贵答案将非常感激

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() 然后发送HTTP连接和获取Jsonobject的URL 问题是这个而不是jSonObject它给出了真正的falue(没有得到原因和方法)

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;

    }
}

之后将其发送给解析器并获得跟随错误

logcat的

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

3 个答案:

答案 0 :(得分:1)

使用以下代码。

 JSONObject json=new JSONObject(result);
      String detail=json.getString("id");
      String image=json.getString("name");  

答案 1 :(得分: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。 希望这会对你有所帮助。

答案 2 :(得分:0)

将Facebook响应设为graphObject并获取其密钥的值。

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

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

facebookResponse是您在onCompleted()

中收到的回复