Jagan
Jagan

Reputation: 642

Arraylist Value object added to the JSON While using GSON

Hi in my app i am using to create a JSON string. So i thought of using GSON. I have two function one is to store the arraylist and another is normal object. My problem is the ArrayList part is working fine but when i print the normal object function the arraylist object is added to the json part here is my coding

main class

  JsonStructure json = new JsonStructure();

             json.messages.add("msg 1");
                json.messages.add("msg 2");
                json.messages.add("msg 3");

                json.asecs.add("asec 1");
                json.asecs.add("asec 2");
                json.asecs.add("asec 3");

            Gson gson = new Gson();
             output = gson.toJson(json);
            Log.i("gson", output);


            List<JsonStructure> Allergylist = new ArrayList<JsonStructure>();
            for (int i = 0; i < 5; i++) {

                JsonStructure oAllergy_Data = new JsonStructure();

                oAllergy_Data.setIdmember("hi");
                oAllergy_Data.setIduser("hello");

                // Adding register to list
                Allergylist.add(oAllergy_Data);

            }
            Gson newgson = new Gson();
            String  newoutput = newgson.toJson(Allergylist);
            Log.i("gson new", newoutput);

and here is my object class

public class JsonStructure {
    String idpeople;
    String iduser;
    String idmember;
    ArrayList<String> messages = new ArrayList<String>();
    ArrayList<String> asecs = new ArrayList<String>();


    public JsonStructure() {

    }

    public String getIdpeople() {
        return idpeople;
    }

    public void setIdpeople(String idpeople) {
        this.idpeople = idpeople;
    }

    public String getIduser() {
        return iduser;
    }
    public void setIduser(String iduser) {
        this.iduser = iduser;
    }
    public String getIdmember() {
        return idmember;
    }
    public void setIdmember(String idmember) {
        this.idmember = idmember;
    }
    public ArrayList<String> getMessages() {
        return messages;
    }
    public void setMessages(ArrayList<String> messages) {
        this.messages = messages;
    }
    public ArrayList<String> getAsecs() {
        return asecs;
    }
    public void setAsecs(ArrayList<String> asecs) {
        this.asecs = asecs;
    }


}

my out put that look like

gson(21917): {"asecs":["asec 1","asec 2","asec 3"],"messages":["msg 1","msg 2","msg 3"]}
gson new(21917): [{"asecs":[],"idmember":"hi","iduser":"hello","messages":[]},{"asecs":[],"idmember":"hi","iduser":"hello","messages":[]},{"asecs":[],"idmember":"hi","iduser":"hello","messages":[]},{"asecs":[],"idmember":"hi","iduser":"hello","messages":[]},{"asecs":[],"idmember":"hi","iduser":"hello","messages":[]}]

my out put should look like

 gson new(21917): [{"idmember":"hi","iduser":"hello"},{"idmember":"hi","iduser":"hello"},{"idmember":"hi","iduser":"hello"},{"idmember":"hi","iduser":"hello"},{"idmember":"hi","iduser":"hello"}]

my question is

1)If the gson will display all the object that declare in the class then why the idpeople is not display only arraylist is displayed to my json

2)Should i need to create a separate object file for the Arraylist or any alter coding for my coding

Upvotes: 0

Views: 807

Answers (1)

whatever the JsonStructure will contain, all that will be part of your json. if you dont want to include it in your json remove it from class.

I personally think it is not going to harm anyone if your json will contain any extra field and that field is useful for you.

Creating an new class -

JsonStructure oAllergy_Data = new JsonStructure();

will allow your arraylist to be a part of your json. if you dont want it to include, make it separate from JsonStructure class.

Or As an alternative Json can be created like this -

JSONObject outer = new JSONObject();
        try {
            // field
            outer.put("EMAIL", reqModel.getEmail());
            // field
            outer.put("PASSWORD", reqModel.getPassword());
        } catch (JSONException e) {
            //
        } catch (Exception e) {
            //
        }
        return outer.toString();

remove this line

ArrayList<String> messages = new ArrayList<String>();
    ArrayList<String> asecs = new ArrayList<String>();

Use

ArrayList<String> messages ;
 ArrayList<String> asecs;

only

Upvotes: 2

Related Questions