0

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?

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

You're wrong as to why it's happening, but I ended up solving it using the Gson libraries.
By casting here I mean result of Object.toString()
2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.