【问题标题】:Android Json Object put a Array JSON without keyAndroid Json Object放了一个没有key的Array JSON
【发布时间】:2020-10-21 03:32:35
【问题描述】:

我需要用 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...当我需要一个值为 Array double 的键“位置”时

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

【问题讨论】:

    标签: android json android-studio android-sdk-tools android-json


    【解决方案1】:

    解决方案

    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
      ]
    }
    

    【讨论】:

      猜你喜欢
      • 2022-01-19
      • 1970-01-01
      • 1970-01-01
      • 2016-07-19
      • 1970-01-01
      • 1970-01-01
      • 2023-02-07
      • 2022-12-01
      • 1970-01-01
      相关资源
      最近更新 更多