简体   繁体   中英

How to send json request in this format

I want to send json request in this format

"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. A JSON string must always start with a { to indicate a JSONObject or with a [ to indicate a 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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