微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Android Json Object放置了一个没有密钥的数组JSON

如何解决Android Json Object放置了一个没有密钥的数组JSON

我需要创建一个具有estructure的json

    {
      "label": "any description","location":[25.7752965,-100.2636682]
    }

Json对象,其数组不包含键,值(位置),

我尝试

String[] _location = new String[2];
_location[0] = String.valueOf(latLng.latitude);
_location[1] = String.valueOf(latLng.longitude);

JSONObject params = new JSONObject();
params.put("location",_location);
params.put("label",_label);

一个尝试使用:

       JsonObject obj = new JsonObject();
       JsonArray array_location = new JsonArray();
       array_location.add(currentLocation.latitude);
       array_location.add(currentLocation.longitude);
       JSONObject params = new JSONObject();
       params.put("location",_location);
       params.put("label",_label);

但是,结果给了我一个带有字符串值的json ...当我需要一个值为数组double的键“位置”时

 {
     "label": "any description","location":"[25.7752965,-100.2636682]"
  }

解决方法

解决方案

JSONObject params = new JSONObject();
params.put("label","any description");

JSONArray locationJsonArray = new JSONArray();
locationJsonArray.put(25.775296); // Replace by your latitude
locationJsonArray.put(-100.2636682); // Replace by your longitude
params.put("location",locationJsonArray);

// For debug purpose
Log.i("DEBUG",params.toString(4));

结果

{
  "label": "any description","location": [
    25.775296,-100.2636682
  ]
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。