简体   繁体   中英

Gson convert Object with ArrayList field to JSON and back

I have a class with two fields.

public class Example {
    private String exampleString;
    private List<Example2> example2List;

    public Example(String exampleString, List<Example2> example2List) {
        this.exampleString = exampleString;
        this.example2List = example2List;
    }

    public String getString() {
        return exampleString;
    }

    public List<Example2> getExample2List() {
        return example2List;
    }
}

In another class I want to convert my Example.class to a JSON String with GSON and convert it back.

public class MainExample {

    public MainExample() {
        Gson mGson = new Gson();

        List<Example2> example2List = new ArrayList<>();
        example2List.add(new Example2());
        example2List.add(new Example2());

        Example example1 = new Example("hello", example2List);

        String resultToJSON = mGson.toJson(example1); //This works fine. The example2List is stored as a JSON Array.

        Example resultFromJSON = mGson.fromJson(resultToJSON, Example.class); // This gives me an error: "Expected BEGIN_OBJECT but was NUMBER at line 1 column 2 path $"
    }
}

I found some solutions on how to deal with JSON Arrays in GSON but I did not find any on how to deal with fields which are lists from an object which should be converted to JSON via Gson.

Any solutions to this? Thanks in advance!

EDITS:

public class Example2 {
    private String string;
    private int integer;

    public Example2(String string, int integer) {
        this.string = string;
        this.integer = integer;
    }

    public String getString() {
        return string;
    }

    public int getInt() {
        return integer;
    }
}

I have tried to recreate the error you have mentioned. The following is the code I have used.

Example.java class :

package co.mr_tpktrustcircle.testgson;

import java.util.List;

public class Example {
    private String exampleString;
    private List<Example2> example2List;

    public String getExampleString() {
        return exampleString;
    }

    public List<Example2> getExample2List() {
        return example2List;
    }

    public Example(String exampleString, List<Example2> example2List) {
        this.exampleString = exampleString;
        this.example2List = example2List;
    }
}

Example2.java class

package co.mr_tpktrustcircle.testgson;

public class Example2 {
    private String string;
    private int integer;

    public Example2(String string, int integer) {
        this.string = string;
        this.integer = integer;
    }

    public String getString() {
        return string;
    }

    public int getInt() {
        return integer;
    }
}

MainExample.java class :

package co.mr_tpktrustcircle.testgson;

import android.util.Log;

import com.google.gson.Gson;

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

public class MainExample {

    public MainExample() {
        Gson mGson = new Gson();
        List<Example2> example2List = new ArrayList<>();
        example2List.add(new Example2("example2.1", 2));
        example2List.add(new Example2("example2.2", 3));

        Example example1 = new Example("hello", example2List);

        String resultToJSON = mGson.toJson(example1); //This works fine. The example2List is stored as a JSON Array.
        Log.i("__json string : ", resultToJSON);
        Example resultFromJSON = mGson.fromJson(resultToJSON, Example.class); // This should give an error as you suggested: "Expected BEGIN_OBJECT but was NUMBER at line 1 column 2 path $"

        Log.i("__resultFromJSON.str : ", resultFromJSON.getExampleString());

        List<Example2> example2FromGson = resultFromJSON.getExample2List();
        for (Example2 example2Element : example2FromGson
                ) {
            Log.d("__example2 element : ",example2Element.getString() + " & " + example2Element.getInt());
        }
    }
}

MainActivity.java class :

package co.mr_tpktrustcircle.testgson;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        callSomething();
    }

    private void callSomething() {
        MainExample mainExample = new MainExample();
    }
}

gradle dependency :

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:24.1.1'
    compile 'com.google.code.gson:gson:2.4'

}

Log :

08-25 22:06:34.430 5906-5906/co.mr_tpktrustcircle.testgson I/__json string :: {"example2List":[{"integer":2,"string":"example2.1"},{"integer":3,"string":"example2.2"}],"exampleString":"hello"}

08-25 22:06:34.435 5906-5906/co.mr_tpktrustcircle.testgson I/__resultFromJSON.str :: hello

08-25 22:06:34.435 5906-5906/co.mr_tpktrustcircle.testgson D/__example2 element :: example2.1 & 2

08-25 22:06:34.435 5906-5906/co.mr_tpktrustcircle.testgson D/__example2 element :: example2.2 & 3

Result :

As you can clearly see from the output(log) there is no problem in your logic . I think you are missing something in your real project.

Although your code is working for me, I can suggest you one thing that might work for you because of difference in package structures.

In your Example class, your member variables are private. Gson is not able to create a pojo because of that. You should have getters and setters in the Example class because these are used by Gson to marshall/unmarshall the Pojo.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM