Java 解析JSON引发异常

Java 解析JSON引发异常,java,json,parsing,Java,Json,Parsing,我试图解析JSON以从中获得几个字段。下面是我的JSON- { "id": "100000224942080000", "name": "Tech Geeky", "first_name": "Tech", "last_name": "Geeky", "link": "https://www.facebook.com/tech.geeky", "username": "tech.geeky", "work": [ { "em

我试图解析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\u name
last\u name
gender
。下面是我写的程序,但它不知怎么抛出了异常。我在这方面做错了什么

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;
    }

}
注意:-
JSON是正确的。在这里发布之前,我对JSON做了一些修改。所以这里的复制粘贴可能出了问题。但假设JSON是正确的。。它仍然会抛出一个异常。

文件末尾附近有一个尾随逗号:

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

在尝试解析JSON之前,可以使用来验证JSON是否有效。有些解析器可能有点松懈,可以原谅一些错误,但大多数都非常严格。

文件末尾附近有一个尾随逗号:

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

在尝试解析JSON之前,可以使用来验证JSON是否有效。有些解析器可能有些松懈,可以原谅一些错误,但大多数都非常严格。

您可以通过如下方式获取字段的数据类型值:

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");

您可以通过如下方式获取字段的数据类型值:

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");

JSON是正确的。在这里发布之前,我对JSON做了一些修改。所以这里的复制粘贴可能出了问题。但假设JSON是正确的。。它仍然抛出异常。你能发布你得到的异常吗?用异常更新问题。我需要从上面的JSON中提取
id、name、first\u name、last\u name、gender
。是否应该
jsonObject.getJSONObject(“id”)
be
jsonObject.get(“id”)
?请看JSON是否正确。在这里发布之前,我对JSON做了一些修改。所以这里的复制粘贴可能出了问题。但假设JSON是正确的。。它仍然抛出异常。你能发布你得到的异常吗?用异常更新问题。我需要从上面的JSON中提取
id、name、first\u name、last\u name、gender
。是否应该
jsonObject.getJSONObject(“id”)
be
jsonObject.get(“id”)
?请参见删除json对象值中的空格。例如,从加利福尼亚州的圣克鲁斯到加利福尼亚州的圣塔克鲁兹。它可能会修复此错误。请删除json对象值中的空格。例如,从加利福尼亚州的圣克鲁斯到加利福尼亚州的圣塔克鲁兹。它可能会修复此错误。