在Android中使用Gson解析json

public class GeRen extends Activity {
private ArrayList s_list;
private TextView type,opendate,name,sex,enddate;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.geren);
initview();
//str为网络请求到的json字符串
String str=this.getIntent().getStringExtra("key");
Log.e("TAG2", str);
Gson gson = new Gson();
Datas d = gson.fromJson(str, Datas.class);
setS_list(d.getResult());
// for(int i=0;i<s_list.size();i++){
// type.setText(s_list.get(i).getType());
// opendate.setText(s_list.get(i).getOpendate());
// name.setText(s_list.get(i).getName());
// sex.setText(s_list.get(i).getSex());
// enddate.setText(s_list.get(i).getEnddate());
// }
}
private void initview() {
type=(TextView) findViewById(R.id.type);
opendate=(TextView) findViewById(R.id.opendate);
name=(TextView) findViewById(R.id.name);
sex=(TextView) findViewById(R.id.sex);
enddate=(TextView) findViewById(R.id.enddate);
}

}

为什么Datas d = gson.fromJson(str, Datas.class);为空 求大神解惑

返回数据错了,应该是{"result_code":0,"result":[{"sex":"男","opendate":"2015-12-02","name":"李飞","type":"","enddate":"2016-12-02"}]}

Gson是一种对象的解析json,很好用,介绍一个网站http://json.parser.online.fr/可以帮我们看一个字符串是不是Json
对于Json文件
{
"id" : "3232",
"data" : {
"data1" : {
"name" : "xiaoming",
"age" : "12"
}
}
}

如果......
答案就在这里:android使用Gson来解析json
----------------------你好,人类,我是来自CSDN星球的问答机器人小C,以上是依据我对问题的理解给出的答案,如果解决了你的问题,望采纳。

估计是Datas这个实体类里面的对象与返回的json数据字段不一致吧。

 public class Datas {
    public ArrayList<result> result;
    private String result_code;
    public ArrayList<result> getResult() {
        return result;
    }
    public void setResult(ArrayList<result> result) {
        this.result = result;
    }
    public String getError_code() {
        return result_code;
    }
    public void setError_code(String result_code) {
        this.result_code = result_code;
    }
    public Datas(ArrayList<result> result,
            String error_code) {
        super();
        this.result = result;
        this.result_code = error_code;
    }



}

{"result_code":0,"result":{"sex":"男","opendate":"2015-12-02","name":"李飞","type":"","enddate":"2016-12-02"}}

这是json串

图片说明这是报错

CSDN问答.

[csdn]www.baidu.cm

把字符串开始和结束的 {}去掉,因为有这个是object而不是array

public ArrayList result;
这应该写错了吧,怎么可能是一个数组来接呢。应该再在里面写一个类Result,里面的对象有sex,opendate,name,type,enddate

应该是你字段不对应的原因。