0

JSONを解析して、そこからいくつかのフィールドを取得しようとしています。以下は私のJSONです-

{
   "id": "100000224942080000",
   "name": "Tech Geeky",
   "first_name": "Tech",
   "last_name": "Geeky",
   "link": "https://www.facebook.com/tech.geeky",
   "username": "tech.geeky",
   "work": [
      {
         "employer": {
            "id": "1854993931353456",
            "name": "Tech"
         },
         "location": {
            "id": "1119482345542155151",
            "name": "Santa Cruz, California"
         },
         "position": {
            "id": "280794135283124256",
            "name": "Senior"
         },
         "start_date": "2012-01"
      }
   ],
   "education": [
      {
         "school": {
            "id": "131182196916370",
            "name": "Fatima School, Gonda"
         },
         "year": {
            "id": "113125125403208",
            "name": "2004"
         },
         "type": "High School"
      }
   ],
   "gender": "male"
}

上記のJSONから、、、、をid抽出nameする必要があります。以下は私が書いた私のプログラムですが、どういうわけか例外をスローします。私はその中で何が間違っているのですか?first_namelast_namegender

public class JSONParser {

    private static final String URL = "https://graph.facebook.com/me?access_token=AAAG2HjMOAsEBAGBhjx2RqqLbOvnAZAxEPQ0X7ZC2JWY0YcQZDZDSSSAFTR";
    private static HashMap<String, String> output = null;

    public static void main(String[] args) throws Exception {

    StringBuilder builder = new StringBuilder();
    DefaultHttpClient httpclient = new DefaultHttpClient();
    output = new HashMap<String, String>();
    BufferedReader bufferedReader = null;
    try {
        HttpGet httpget = new HttpGet(URL);
        httpget.getRequestLine();
        HttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();
        //System.out.println(response.getStatusLine());
        if (entity != null) {
        InputStream inputStream = entity.getContent();
        bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        for (String line = null; (line = bufferedReader.readLine()) != null;) {
            builder.append(line).append("\n");
        }
        JSONObject jsonObject = new JSONObject(builder.toString());
        JSONObject info = jsonObject.getJSONObject("id");
        parseJSONObject(info, output);
        }
    } catch (Exception e) {

    } finally {
        try {
        bufferedReader.close();
        httpclient.getConnectionManager().shutdown();
        } catch (IOException e) {

        }
    }
    }

    private static HashMap<String, String> parseJSONObject(JSONObject json,
        HashMap<String, String> output) throws JSONException {

    Iterator<String> keys = json.keys();
    while (keys.hasNext()) {
        String key = keys.next();
        String val = null;
        try {
        JSONObject value = json.getJSONObject(key);
        parseJSONObject(value, output);
        } catch (Exception e) {
        val = json.getString(key);
        }
        if (val != null) {
        output.put(key, val);
        }
    }
    return output;
    }

}

例外:-

org.json.JSONException: JSONObject["id"] is not a JSONObject.

注: -JSONは正しいです。ここに投稿する前に、JSONを少し変更しました。したがって、ここに貼り付けをコピーする際に問題が発生した可能性があります。しかし、JSONが正しいと仮定すると..それでも例外がスローされます。

4

2 に答える 2

3

次のようにデータ型の値を取得することで、フィールドを取得できます。

int id = jsonObject.getInt("id");
String name = jsonObject.getString("name");
String firstName = jsonObject.getString("first_name");
String lastName = jsonObject.getString("last_name");
String gender = jsonObject.getString("gender");
于 2013-01-27T00:21:44.190 に答える
1

ファイルの終わり近くに末尾のコンマがあります:

"gender": "male",
----------------^

JSONLintを使用して、JSONを解析する前にJSONが有効であることを確認できます。一部のパーサーは多少緩く、エラーを許容する場合がありますが、ほとんどは非常に厳密です。

于 2013-01-27T00:13:08.083 に答える