阅读2d Json对象android

时间:2015-02-09 21:18:44

标签: android json

你好,我有这个json:

{"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的帮助

1 个答案:

答案 0 :(得分:0)

如果你总是得到m1,那么marker.getId()总是返回m1。确保你输入了正确的标记,并且你的getId函数是正确的。

另请注意,由于m0保存为false,因此您实际上不会在视图上设置任何文本(您将获得所有空白)。