I can't seem to find this anywhere, but tons of people have to be doing this.
I have an array of objects that I want to convert to a JSON String and post to a REST URL. Here's what I have so far:
if(history==null||history.length == 0){
return new String[0];
}
JSONArray array = new JSONArray();
for(DeviceHistory connectHistory:history){
array.put(connectHistory);
}
JSONObject response = jsonClient.remoteCall(SERVICE_NAME, array.toString());
The problem is that I get ["com.abc.model.connect.DeviceHistory@41e63298","com.abc.model.connect.DeviceHistory@41e63760","com.abc.model.connect.DeviceHistory@41e63c28","com.abc.model.connect.DeviceHistory@41e640f0"] from array.toString(). What am I doing wrong?
Your problem is that you are not passing your object as a String, so what you are writing in your JSON is a reference to your object.
You should implement your toString()
method in that class if you can or just use it. However, if you cant you will need a helper method to achieve it.
Because this is the result of Object.toString()
. You may want to try:
https://code.google.com/p/google-gson/
This library allows to convert Object to JSON and back.
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.