2011-11-04 56 views
1

你好再次,這是我的第二個問題。這是我的第一個question。由傢伙給出的答案是真正有用的,所以我想在這裏實現it.And是我實現..JSON解析錯誤谷歌地圖的地方api

製作JSONFunction類與靜態方法whcch將返回JSON 對象

public class JSONFunction { 

public static JSONObject getJSONfromURL(String url){ 
    InputStream is = null; 
    String result = ""; 
    JSONObject jArray = null; 

    //http post 
    try{ 
      HttpClient httpclient = new DefaultHttpClient(); 
      HttpPost httppost = new HttpPost(url); 
      HttpResponse response = httpclient.execute(httppost); 
      HttpEntity entity = response.getEntity(); 
      is = entity.getContent(); 

    }catch(Exception e){ 
      Log.e("log_tag", "Error in http connection "+e.toString()); 
    } 

    //convert response to string 
    try{ 
      BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8); 
      StringBuilder sb = new StringBuilder(); 
      String line = null; 
      while ((line = reader.readLine()) != null) { 
        sb.append(line + "\n"); 
      } 
      is.close(); 
      result=sb.toString(); 
    }catch(Exception e){ 
      Log.e("log_tag", "Error converting result "+e.toString()); 
    } 

    try{ 

     jArray = new JSONObject(result);    
    }catch(JSONException e){ 
      Log.e("log_tag", "Error parsing data "+e.toString()); 
    } 

    return jArray; 
} 
} 

我現在的主要活動是..

public class JsonExampleActivity extends ListActivity { 
/** Called when the activity is first created. */ 
JSONFunction JSONfunction; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.listplaceholder); 

    ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>(); 


    JSONObject json = JSONFunction.getJSONfromURL("https://maps.googleapis.com/maps/api/place/search/json?location=-33.8670522,151.1957362&radius=1000&types=resturants&sensor=false&key=0bBgLl42nWwl7TQHrAFpY89v2FeLlijIGTLJ1AA"); 

    try{ 
     JSONObject httpattributr= json.getJSONObject("html_attributions"); 
     //JSONObject results =new JSONObject("results"); 
     JSONArray JArray = httpattributr.getJSONArray("results"); 

     for(int i=0;i<JArray.length();i++){      
      HashMap<String, String> map = new HashMap<String, String>();  
      JSONObject e = JArray.getJSONObject(i); 
      map.put("id", String.valueOf(i)); 
      map.put("name", "" + e.getString("name")); 
      map.put("type", "type: " + e.getString("type")); 
      mylist.add(map);    
     }  
    }catch(JSONException e)  { 
     Log.e("log_tag", "Error parsing data "+e.toString()); 
    } 

    ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.main, 
        new String[] { "name", "type" }, 
        new int[] { R.id.item_title, R.id.item_subtitle }); 

    setListAdapter(adapter); 

    final ListView lv = getListView(); 
    lv.setTextFilterEnabled(true); 
    lv.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) {    
      @SuppressWarnings("unchecked") 
      HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);     
      Toast.makeText(JsonExampleActivity.this, "ID '" + o.get("id") + "' was clicked.", Toast.LENGTH_SHORT).show(); 

     } 
    }); 
} 
} 

但是我得到錯誤的logcat作爲

11-04 19:56:53.099: ERROR/log_tag(298): Error parsing data org.json.JSONException: JSONObject["html_attributions"] is not a JSONObject. 

任何幫助將高度讚賞提前
感謝。

JSON結果看起來像

{ 
"html_attributions" : [ 
"Listings by \u003ca href=\"http://www.yellowpages.com.au/\"\u003eYellow Pages\u003c/a\u003e" 
    ], 
"results" : [ 
{ 
    "geometry" : { 
    "location" : { 
     "lat" : -33.8719830, 
     "lng" : 151.1990860 
    } 
    }, 
    "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png", 
    "id" : "677679492a58049a7eae079e0890897eb953d79b", 
    "name" : "Zaaffran Restaurant - BBQ and GRILL, Darling Harbour", 
    "rating" : 3.90, 
    "reference" : "CpQBjAAAAHDHuimUQATR6gfoWNmZlk5dKUKq_n46BpSzPQCjk1m9glTKkiAHH_Gs4xGttdOSj35WJJDAV90dAPnNnZK2OaxMgogdeHKQhIedh6UduFrW53wtwXigUfpAzsCgIzYNI0UQtCj38cr_DE56RH4Wi9d2bWbbIuRyDX6tx2Fmk2EQzO_lVJ-oq4ZY5uI6I75RnxIQJ6smWUVVIHup9Jvc517DKhoUidfNPyQZZIgGiXS_SwGQ1wg0gtc", 
    "types" : [ "restaurant", "food", "establishment" ], 
    "vicinity" : "Harbourside Centre 10 Darling Drive, Darling Harbour, Sydney" 
}, 

回答

0

變化

JSONObject httpattributr= json.getJSONObject("html_attributions"); 
JSONArray JArray = httpattributr.getJSONArray("results"); 

JSONArray httpattributr= json.getJSONArray("html_attributions"); 
JSONArray JArray = json.getJSONArray("results"); 

編輯:結果數組不是孩子或html_attributions數組中的元素 - 他們是兄弟姐妹。

0

在,你解析,也不能是一個關鍵名爲「html_attributions」的JSON。你的json是什麼樣的?

+0

嘿裴家我這裏張貼的JSON format.and是鏈接http://code.google.com/apis/maps/documentation/places/#PlaceSearchResponses –

1

「html_attributions」 鍵具有JSONArray,因爲它的價值,所以不是

JSONObject httpattributr= json.getJSONObject("html_attributions"); 

嘗試使用...

JSONArray httpattributtr = json.getJSONArray("html_attributions"); 
+0

其實JSONFunction reurn一個JSONOject所以,我怎麼可以轉換的JSONObject到JSONArray和我編輯它的錯誤soory。 –