0

I am trying to construct a Json Array which contains Json Objects. I remember doing this some time ago with org.json but now i am having trouble doing it with gson. The structure for my json array should be like:

[
    {
        "colour": "abc",
        "time": "xyz
    },
    {
        "colour": "abc",
        "time": "xyz"
    }
]

i am reading the length and area values from a database by looping through json format files that i got using .get() thanks to jersey. I have managed to form internal json objects {"colour": "xyz", "time": "abc" }. What i have done is stored these objects in an array of json objects. Then i have tried using gson.toJson method to form the array containing these objects. Although doing so gives me something close to my requirement but i want it to be more compact. Here is the json array that i managed to generate.

 [
{
    "members": {
        "colour": {
            "value": "xyz"
        },
        "time": {
            "value": "abc"
        }
    }
},
{
    "members": {
        "colour": {
            "value": "abc"
        },
        "time": {
                "value": "xyz"
            }
        }
    }
]

I want to get rid of the redundant "members" and "value" keywords.

My code for this json array is

JsonObject[] innerObjJson = new JsonObject[hits];

for (int i=0;i<hits;i++){
        String colour  = jsonVariable.getAsJsonObject().get(i).getAsJsonObject().get("colour").toString();
        String time  = jsonVariable.getAsJsonObject().get(i).getAsJsonObject().get("time").toString();


        colour = colour.replaceAll("\"" ,"");
        time = colour.replaceAll("\"" ,"");


        InnerJsonStructure innerObj = new InnerJsonStructure(colour,time);

        Gson gson = new Gson();
        String innerObjString = gson.toJson(innerObj);
        JsonParser parserNew = new JsonParser();
        innerObjJson[i] = (JsonObject)parserNew.parse(innerObjString);
}


        Gson gson1 = new Gson();
        String finalJsonArr = gson1.toJson(innerObjJson);
        System.out.println(finalJsonArr);

The InnerJsonStructure is

public class InnerJsonStructure {
    public String colour;
    public String time;



    InnerJsonStructure(String Color, String Time){
        this.colour = Color;
        this.time = Time;


    }
}

Any help?

2
  • What is the final format you expect? Commented Aug 28, 2015 at 15:10
  • the first json in my question, that is what i expect.. where i just have an array with objects { "colour": "abc", "time": "xyz" } Commented Aug 28, 2015 at 15:30

1 Answer 1

1

You need to define an object data base, to put together a list of the object, something like this:

public class DataObject {

    private String colour = "";
    private String time = "";

    public String getColour() {
        return colour;
    }

    public void setColour(String colour) {
        this.colour = colour;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

}

import java.io.FileWriter;
import java.io.IOException;
import com.google.gson.Gson;
import java.util.ArrayList;
import java.util.List;

public class GsonExample {

    public static void main(String[] args) {

        DataObject obj1 = new DataObject();
        obj1.setColour("abc");
        obj1.setTime("12:00");

        DataObject obj2 = new DataObject();
        obj1.setColour("def");
        obj1.setTime("24:00");

        List<DataObject> lista = new ArrayList<>();
        lista.add(obj1);
        lista.add(obj2);

        Gson gson = new Gson();
        String json = gson.toJson(lista);

        try {
            FileWriter writer = new FileWriter("c:\\file.json");
            writer.write(json);
            writer.close();

        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println(json);

    }
}

When the code is executed, the console output is as follows:

[{"colour":"abc","time":"12:00"},{"colour":"def","time":"24:00"}]

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

1 Comment

thanks. i saw my mistake immediately. I was passing an array of objects to gson, but i needed to pass a list of objects to it. Now it works perfectly.

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.