Gson将带有ArrayList字段的Object转换为JSON并返回

时间:2016-08-25 13:01:58

标签: java android json gson

我有一个有两个字段的课程。

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;
    }
}

在另一个类中,我想将我的Example.class转换为带有GSON的JSON字符串并将其转换回来。

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 $"
    }
}

我找到了一些关于如何在GSON中处理JSON数组的解决方案但我没有找到任何关于如何处理来自对象的列表的字段,这些字段应该通过Gson转换为JSON。

对此有何解决方案?提前谢谢!

编辑:

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;
    }
}

2 个答案:

答案 0 :(得分:0)

尝试重新创建您提到的错误。以下是我使用的代码。

Example.java类:

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类

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类:

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类:

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依赖:

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'

}

记录:

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

结果:

正如您可以从输出(日志)中清楚地看到,逻辑中没有问题。我认为你在实际项目中遗漏了一些东西。

答案 1 :(得分:0)

虽然您的代码对我有用,但我可以建议您一件可能对您有用的事情,因为包结构不同。

在Example类中,您的成员变量是私有的。因此,Gson无法创造一个pojo。你应该在Example类中有getter和setter,因为Gson使用它们来策划/解组Pojo。