简体   繁体   English

如何以这种格式发送json请求

[英]How to send json request in this format

I want to send json request in this format 我想以这种格式发送json请求

"coordinates":{"0":{"received_at":1471529314,"lat":28.6888798,"long":77.3247225}}

but i am able to send the request in this format 但我能够以这种格式发送请求

{"coordinates":{"0":{"received_at":1471529314,"lat":28.6888798,"long":77.3247225}}}

I am using this code 我正在使用此代码

            jsonObject.put("received_at", now);
            jsonObject.put("lat", latitude);
            jsonObject.put("long", longitude);
            JSONObject json = new JSONObject();
            json.put("0",jsonObject);
            jsonObject1 = new JSONObject();
            jsonObject1.put("coordinates",json);
            Log.e("JSON",jsonObject1.toString());

The first request you are sharing is not valid JSON. 您共享的第一个请求不是有效的JSON。 A JSON string must always start with a { to indicate a JSONObject or with a [ to indicate a JSONArray. JSON字符串必须始终以{表示JSONObject或以[表示JSONArray。
If you insist on having the first format, I think you would have to build that string by yourself 如果您坚持使用第一种格式,那么我认为您必须自己构建该字符串

要删除第一个和最后一个括号,请使用此

String specialString = jsonObject1.toString().substring(1, jsonObject1.toString().length() - 1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM