-1

Facebook JSon データの解析に問題があります。私のデータ形式はこのようなものです。これに多くの時間を費やしましたが、値が得られませんでした。この問題の解決方法を教えてください。貴重な回答をお待ちしております。

ジェイソン

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

Actullay を 更新 します 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

Facebook Response を として作成しgraphObject、そのキーの値を取得します。

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 に答える
0

UPDATE : 関数をこれに置き換えます

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 に答える