Venu Gopal
Venu Gopal

Reputation: 15

how to convert list of objects to json using gson when object has ArrayList attribute

I am trying to store some list of objects in sharedpreference when my app is first time launched and i am going to use this list of objects in subsequent usage of my app. myclass:

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

public class MyClass {
    private int a;
    private String b;
    private int c;
    private ArrayList<NameValuePair> d;

    public MyClass(int a, String b, int c) {
        super();
        this.a = a;
        this.b = b;
        this.c = c;

    }

    public MyClass(int a, String b, int c, ArrayList<NameValuePair> d) {
        super();
        this.a = a;
        this.b = b;
        this.c = c;
        this.d = d;
    }

    public int getA() {
        return a;
    }

    public void setA(int a) {
        this.a = a;
    }

    public String getB() {
        return b;
    }

    public void setB(String b) {
        this.b = b;
    }

    public int getC() {
        return c;
    }

    public void setC(int c) {
        this.c = c;
    }

    public ArrayList<NameValuePair> getD() {
        return d;
    }

    public void setD(ArrayList<NameValuePair> d) {
        this.d = d;
    }

    public static List<MyClass> getMyObjects() {
        // TODO Auto-generated method stub
        List<MyClass> myObjects = new ArrayList<MyClass>();

        ArrayList<NameValuePair> temp1 = new ArrayList<NameValuePair>();
        temp1.add(new BasicNameValuePair("1", "20"));
        temp1.add(new BasicNameValuePair("2", "30"));
        temp1.add(new BasicNameValuePair("3", "40"));

        ArrayList<NameValuePair> temp2 = new ArrayList<NameValuePair>();
        temp2.add(new BasicNameValuePair("1", "50"));
        temp2.add(new BasicNameValuePair("2", "60"));
        temp2.add(new BasicNameValuePair("3", "70"));

        ArrayList<NameValuePair> temp3 = new ArrayList<NameValuePair>();
        temp3.add(new BasicNameValuePair("1", "50"));
        temp3.add(new BasicNameValuePair("2", "60"));
        temp3.add(new BasicNameValuePair("3", "70"));

        myObjects.add(new MyClass(1, "ABC", 20, temp1));
        myObjects.add(new MyClass(2, "DEF", 30, temp2));
        myObjects.add(new MyClass(3, "GHI", 40, temp3));

        return myObjects;
    }
}

i've created the list of objects and stored in sharedpreference using following lines of code:

 List<MyClass> myObjects = MyClass.getmyObjects();
    String myObjectsJSONString = new Gson().toJson(myObjects);
    prefsEditor.putString("MYOBJECTS", myObjectsJSONString);

JSON string formed as expected while writing myObjects to sharedpreference sample:

 [{"d":[{"name":"1","value":"20"},{"name":"2","value":"30"},{"name":"3","value":"40"}],"b":"ABC","c":20,"a":1},{"d":[{"name":"1","value":"50"},{"name":"2","value":"60"},{"name":"3","value":"70"}],"b":"DEF","c":30,"a":2},{"d":[{"name":"1","value":"50"},{"name":"2","value":"60"},{"name":"3","value":"70"}],"b":"GHI","c":40,"a":3}]

in subsequent usage i am trying to populate my list using following lines of code:

String myobjectsJSONString =appPrefs.getString("MYOBJECTS", null);
Type type = new TypeToken < List <MyClass>> () {}.getType();
List <MyClass> myObjects = new Gson().fromJson(myobjectsJSONString, type);

I am able to store and generate my list of objects from sharedpreferences without attribute "d", which is of ArrayList<NameValuePair> type;

But, with "d" as one of the attributes, i am unable to generate the object.

Error shown is : java.lang.RuntimeException: Unable to invoke no-args constructor for interface org.apache.http.NameValuePair. Register an InstanceCreator with Gson for this type may fix this problem

the above exception raised while trying to populate objects from the json stored in sharedpreference

Upvotes: 0

Views: 2719

Answers (2)

david.mihola
david.mihola

Reputation: 13002

I think I get it now: Your MyClass includes a field

private  ArrayList<NameValuePair> d;

NameValuePair however, is actually Apache's org.apache.http.NameValuePair, which is an interface. As such, Gson cannot create instances of it - it can only create instances of a class.

You could try changing that to

private  ArrayList<BasicNameValuePair> d;

but that class doesn't seem to have a no-arguments constructor, so it might not work either (this answer, though, says it should: https://stackoverflow.com/a/18645370/763935).

So your safest bet might be to write your own class implementing NameValuePair:

public class MyNameValuePair implements NameValuePair {
    private final String name;
    private final String value;

    public MyNameValuePair() {
        this.name = null;
        this.value = null;
    }

    @Override
    public String getName() {
        return this.name;
    }

    @Override
    public String getValue() {
        return this.value;
    }
}

Upvotes: 1

Ravind Maurya
Ravind Maurya

Reputation: 977

You can do like this:

{
    "Result": [{
        "name": "1",
        "value": "100"
    }, {
        "name": "1",
        "value": "100"
    }, {
        "name": "1",
        "value": "100"

    }]
}

use :

Type listType = new TypeToken<List<BeanClass.ResultEntity>>() {
            }.getType();

            List<BeanClass.ResultEntity> list = new Gson().fromJson(response.toString(), listType);

It will work definitely...

Upvotes: 0

Related Questions