Android Json 对象放置一个没有键的数组 JSON

Android Json Object put a Array JSON without key

我需要用结构

创建一个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...当我需要一个值为 Array 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
  ]
}