android解析json数据

项目中要用到json数据格式,偷闲写了个demo出来,数据是从txt文件中读取的。

代码就两段:

json解析方法

 public void json2Str(String src) throws JSONException{
        JSONObject jsonObject = new JSONObject(src);
        String id;
        String sendName;
        String sendTime;
        String title;
        String source;
        String url;
        String postTime;
        String content;
        
        id = jsonObject.getString("id");
        sendName = jsonObject.getString("senderName");
        sendTime = jsonObject.getString("sendTime");
        title = jsonObject.getString("title");
        source = jsonObject.getString("source");
        url = jsonObject.getString("url");
        postTime = jsonObject.getString("posttime");
        content = jsonObject.getString("content");
        txt.setText(":" + id + "\n" + ":" + sendName + "\n" + ":" + sendTime + "\n" + ":" + title
                    + "\n" + ":" + source + "\n" + ":" + url + "\n" + ":" + postTime
                    + "\n" + ":" + content);
                
    }

读取txt文件:

String src = null;            
                 InputStream in;
                 try{                    
                       in = getResources().openRawResource(R.raw.json);
                       int length = in.available();       
                       byte [] buffer = new byte[length];        
                       in.read(buffer);
                       src = EncodingUtils.getString(buffer, "GBK");
                       in.close();                                    
                    }
                    catch (Exception e){
                       e.printStackTrace();
                    }
                    
                etx.setText("已加载内容");
                
                try {
                    json2Str(src);
                } catch (JSONException e) {
                    e.printStackTrace();
                }



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值