简体   繁体   中英

how to convert json into string without line break and the other format

I'm setting up a new server and android, and want to send base64(image) from android to server. I use JSON for data format between android and server, so I have to put my base64 into that JSON, but the server cannot decode my base64 because there are a symbol like "\\n,+," and many more. I also try using Notepad for replace "\\n" but still the server wont decode it..

note: if I use Postman the server work properly

JSONArray data = new JSONArray();

for (int i = 0; i < listImage.size(); i++) {
    //listImage.get(i).setBase64(map.get(listImage.get(i).getId()));
    jsonObject = new JSONObject();
    jsonObject.put("id",listImage.get(i).getId());
    jsonObject.put("base64",map.get(listImage.get(i).getId()));
    data.put(jsonObject);
}

jsonObject = new JSONObject();
jsonObject.put("image",data);

JSONObject has toString() method that return json object data into String format. You can also look at UTF encoding to encode special character, to make http request without conflicting of special character in URL.

jsonObject = new JSONObject();
jsonObject.put("image",data);
jsonObject.toString();

Easiest way to convert jason object to a String

JSONObject json = new JSONObject();
json.toString();

谢谢您的回答...我已经找到了自己的解决方案,我需要在服务器端使用base64中的“”替换“ \\ n”和“ \\”

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