xdx1399513637 2015-12-02 07:19 采纳率: 100%
浏览 1924
已采纳

在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);为空 求大神解惑

  • 写回答

10条回答 默认 最新

  • c小旭 2015-12-02 08:28
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • Robot-C 2015-12-02 07:23
    关注

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

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

    评论
  • keyborad_boy 2015-12-02 08:13
    关注

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

    评论
  • xdx1399513637 2015-12-02 08:16
    关注
     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串

    评论
  • xdx1399513637 2015-12-02 08:23
    关注

    图片说明这是报错

    评论
  • 纸上得来终觉浅 2015-12-02 08:30
    关注
    评论
  • 纸上得来终觉浅 2015-12-02 08:30
    关注
    评论
  • scrazying 2015-12-03 01:28
    关注

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

    评论
  • keyborad_boy 2015-12-03 07:53
    关注

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

    评论
  • 月盡天明 博客专家认证 2015-12-04 03:46
    关注

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

    评论
查看更多回答(9条)

报告相同问题?

悬赏问题

  • ¥15 c++图Djikstra算法求最短路径
  • ¥15 Linux操作系统中的,管道通信问题
  • ¥15 ansible tower 卡住
  • ¥15 等间距平面螺旋天线方程式
  • ¥15 通过链接访问,显示514或不是私密连接
  • ¥100 系统自动弹窗,键盘一接上就会
  • ¥50 股票交易系统设计(sql语言)
  • ¥15 调制识别中这几个数据集的文献分别是什么?
  • ¥15 使用c语言对日志文件处理
  • ¥15 请大家看看报错原因,为啥会这样