簡體   English   中英

JSON解析錯誤Google Map Place API

[英]JSON parsing error google map place api

您好,這是我的第二個問題。 這是我的第一個問題 伙計們給出的答案非常有幫助,所以我嘗試實現它。這是我的實現。

使用靜態方法whcch制作JSONFunction類將返回json Object

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"
},

“ html_attributions”鍵的值為JSONArray,因此它不是

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

嘗試使用...

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

在您要解析的json中,一定不能有一個名為“ html_attributions”的鍵。 您的json是什么樣的?

更改

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

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM