Question

How can I concatenate multiple JsonRepresentation Object into one, without building my own string parser?

Say I have two JsonRepresentation objects

obj1 = {"name":"obj1"};
obj2 = {"name":"obj2"};

I would like to get the result concatenation as:

 {
    {"name":"obj1"},
    {"name":"obj2"}
 } 

Reading the JsonRepresentation, there is no easy way to do this except by doing some string manipulation. Am I right?

Thanks

Was it helpful?

Solution

If you're referring to this JsonRepresentation class, and you want to merge the 2 objects into an array, then you should be able to do it as follows:

JSONObject jsonObj1 = obj1.toJsonObject();
JSONObject jsonObj2 = obj2.toJsonObject();
JSONArray jsonArray = new JSONArray().append(jsonObj1).append(jsonObj2);
JsonRepresentation jsonConcat = new JsonRepresentation(jsonArray);

Note: I haven't actually used the library, but if it behaves per the API, this should be pretty straightforward.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top