2015-02-09 102 views
0

你好,我有這樣的JSON:閱讀2D JSON對象的Android

{"m1":     
    {"phone":"510000", 
    "lon":-75.5898195, 
    "postal":"050022", 
    "address":"cll 19", 
    "company":"my company", 
    "saved":true, 
    "address2":"adress my company", 
    "lat":6.2418782}, 
"m0": 
    {"phone":"85555555", 
    "lon":-75.5898195,' 
    "postal":"45895", 
    "address":"cll 65 d 54", 
    "company":"c1", 
    "saved":false, 
    "address2":"jhvbf", 
    "lat":6.2418782} 
} 

現在,當我在谷歌地圖的標記點擊,這些信息應該出現在一個抽屜佈局傳遞標記上檢索標記ID該函數:

private void openDrawerLayout(final Marker marker){ 
    try { 
     JSONObject inf = markerId.getJSONObject(marker.getId()); 
     //JSONObject inf = (JSONObject) markerId.get(marker.getId()); 
     if(inf.getBoolean("saved")){ 
      String companyJs = inf.getString("company"); 
      String address1Js = inf.getString("address"); 
      String address2Js = inf.getString("address2"); 
      String postalJs = inf.getString("postal"); 
      String phoneJs = inf.getString("phone"); 
      company.setText(companyJs); 
      address1.setText(address1Js); 
      address2.setText(address2Js); 
      postal_code.setText(postalJs); 
      phone.setText(phoneJs); 
     }else{ 
      address1.setText(""); 
      address2.setText(""); 
      postal_code.setText(""); 
      phone.setText(""); 
     } 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 

當我使用這個功能markerId.getJSONObject(marker.getId()),被檢索永諾第一JSON對象(M1),需要關於如何獲取正確的ID標記JSON幫助

回答

0

如果你總是得到m1,那麼marker.getId()總是返回m1。確保你傳遞了正確的標記,並且你的getId函數是正確的。

另請注意,因爲在m0上保存的是錯誤的,所以您不會在視圖上真正設置任何文本(您會得到所有空白)。