2

I tried this:

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiResponse;
import com.wordnik.swagger.annotations.ApiResponses;
import org.apache.commons.lang.Validate;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.print.attribute.standard.Media;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.*;
import javax.ws.rs.core.*;
import java.io.Serializable;

public static class MyJSON implements Serializable {
    private final String name = "myname";

    // **Why don't I get this field serialized in the response?**
    private final JSONObject jsonObject = new JSONObject();

    public MyJSON() {
        try {
            jsonObject.put("mykey", "myvalue");
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public String getName() { return name; }
    public JSONObject getJsonObject() { return jsonObject; }

}

@GET
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Get all entities", notes = "get all entities", response = Response.class)
@ApiResponses(value = { @ApiResponse(code = 200, message = "OK status"),

public Response getList() {

    return Response.ok(new MyJSON(), MediaType.APPLICATION_JSON).build();
}

the response I get:

{
  "name": "myname"
}

as you see I get only the name field of MyJSON without the jsonObject field.

any ideas how can I get the jsonObject fields also serialized?

UPDATE:

after reading Thomas comment I tried using a map:

public static class MyJSON implements Serializable {
    private final String name = "myname";
    private final Map somefield = new HashMap();

    public String getName() { return name; }
    public Map getSomefield() { return somefield; }

    public void addOther(String key, String value) {
        somefield.put(key, value);
    }

}

        MyJSON myJSON = new MyJSON();
        myJSON.addOther("mhykey", "myvalue");
        return Response.ok(myJSON, MediaType.APPLICATION_JSON).build();

Now I get again:

{
  "name": "myname" // where is the other field? (the map)
}

I wonder again why doesn't it serializes it? please note I can't use specific objects because the json can vary at one scenario certain fields at another scenario other fields, I can't create a new class for each such case.

6
  • 1
    Did you try using a plain Map<String, Object> instead of JSONObject? Since the pojo is serialized to json the mapper might have problems when encountering JSONObjects directly. Besides that it might be better to use specific objects anyways, i.e. instead of a map you could provide a nested pojo that has the mykey field. Commented Aug 3, 2016 at 14:30
  • 1
    How would you like it to be serialized? Commented Aug 3, 2016 at 14:49
  • @Thomas updated question (see UPDATE) according to your suggestion. Commented Aug 3, 2016 at 15:05
  • @SotiriosDelimanolis { "name": "value", "mykey": "myvalue" } Commented Aug 3, 2016 at 15:06
  • 1
    I see you have the Gson tag, however you are not using it. I'm going to remove that tag. Commented Aug 3, 2016 at 15:21

1 Answer 1

3

If this is how you want the class to be serialized

{
    "name": "value",
    "mykey": "myvalue"
}

Then this is how your object should look like

class Data { 
    String name, String mykey;

    // getters, setters...
}

Alternatively, when @Thomas said, a HashMap, he did not mean "nest" a HashMap into the Object, he literally meant use a HashMap, though, not all JSON libraries support that constructor.

HashMap<String, String> data = new HashMap<String, String>(); 
data.put("name", "value");
data.put("mykey", "myvalue");

JSONObject json = new JSONObject(data);
String jsonString = json.toString();

Another thing you could do is just treat your object as a JSONObject itself.

class Data extends JSONObject {

    public Data() { }

}
Data d = new Data();
d.put("name", "value");

Though, that seems silly.

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

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.