-1

我有以下回应:

{
   "data": [
      {
         "name": "This is a test 123",
         "start_time": "2013-12-02T18:00:00+0530",
         "end_time": "2013-12-02T20:00:00+0530",
         "location": "Mount Lavinia",
         "id": "525447507473743",
         "rsvp_status": "attending"
      },
      {
         "name": "This is a test event",
         "start_time": "2013-12-02T18:00:00+0530",
         "end_time": "2013-12-02T20:00:00+0530",
         "location": "Mount Lavinia",
         "id": "560383743988530",
         "rsvp_status": "attending"
      },
}

],

我有以下代码来访问响应:

                JSONObject jsonObj = new JSONObject(result);
                JSONArray jsonArr = jsonObj.getJSONArray("data");
                for(int i = 0; i < jsonArr .length(); i++)
                {
                   JSONObject jsonArrObj = jsonArr.getJSONObject(i);
                   String id = jsonArrObj.getString("id");
                   String username = jsonArrObj.getString("name");
                   Log.w("event names are", username.toString());

                } 

我收到以下错误:

json.JSON.typeMismatch(JSON.java:100)

我搜索了一个解决方案,但找不到任何解决方案。

4

1 回答 1

1

这是您从 graph.facebook.com API 获得的 JSON 字符串的完整解析:

ArrayList<HashMap<String,String>> arrahaspmap=new 
                              ArrayList<HashMap<String,String>>();
HashMap<String,String> hashmaptemp;
JSONObject jsonObj;
 try {
    jsonObj = new JSONObject(sdskd);
    JSONArray jsonArr = jsonObj.getJSONArray("data");
    System.out.println("jsonArr jsonArr jsonArr jsonArr :: "+jsonArr.length());

    for(int i = 0; i < jsonArr .length(); i++)
         {
           JSONObject jsonArrObj = jsonArr.getJSONObject(i);
           hashmaptemp=new HashMap<String, String>();
           hashmaptemp.put("id", jsonArrObj.getString("id"));
           hashmaptemp.put("end_time", jsonArrObj.getString("end_time"));
           hashmaptemp.put("location", jsonArrObj.getString("location"));
           hashmaptemp.put("name", jsonArrObj.getString("name"));
           hashmaptemp.put("rsvp_status", jsonArrObj.getString("rsvp_status"));
           arrahaspmap.add(hashmaptemp);
        } 
  JSONObject jsonobjpaging= jsonObj.getJSONObject("paging");
  String strprevious=jsonobjpaging.getString("previous");
  String strnext=jsonobjpaging.getString("next");
  System.out.println("jsonobjpaging previous :: "+strprevious);

  System.out.println("strnext next :: "+strnext);
} catch (JSONException e) {
   // TODO Auto-generated catch block
     e.printStackTrace();
}
于 2012-12-05T09:28:36.587 回答