相关疑难解决方法(0)

GSON中的JSON数组解析,但对象为空

我试图用json将json字符串转换为对象.

我在下面有一个非常简单的例子,它运行,但结果答案是空的,即:我的答案对象的文本字段为空.

import com.google.gson.*;

public class Meow {

    public static void main(String[] args) throws Exception{

        Gson gson = new Gson();
        String jsonOutput = "[{\"answer\":{\"text\":\"text1\"}},{\"answer\":{\"text\":\"text2\"}} ]";

        Answer[] a = gson.fromJson(jsonOutput, Answer[].class);

        for(Answer i:a) {
          System.out.println(i.text);
        }       
    }

    public class Answer {

        public String text;

        public Answer(String text) {
            super();
            this.text=text;
        }

        public String toString(){
            return text;
        }

        public void setText(String a){
            this.text=a;
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

java android json gson

2
推荐指数
1
解决办法
2247
查看次数

标签 统计

android ×1

gson ×1

java ×1

json ×1