Json解析的三种方式

这篇博客介绍了Java中解析JSON的三种方法:1) 使用org.json包,通过JSONObject和JSONArray处理对象和数组;2) 利用JsonReader进行类似PULL解析XML的操作,需注意游标的正确移动;3) 使用GSON库,通过简单的步骤和JavaBean映射快速解析。每种方法都易于理解和使用。
摘要由CSDN通过智能技术生成
解析JSON
  • 方式一:使用org.json包解析
/**
     * 通过org.json解析json
     * @param jsonStr json字符串
     * @throws JSONException  格式不对,转换异常
     */
    public static Sentence parseJsonByOrgJson(String jsonStr) throws JSONException{
        // 使用该方法解析思路,遇到大括号用JsonObject,中括号用JsonArray
        // 第一个是大括号{}
        JSONObject jsonObj = new JSONObject(jsonStr);
        // 新建Sentence对象
        Sentence sentence = new Sentence();
        // 以下是无脑操作
        String caption = jsonObj.getString("caption");
        String content = jsonObj.getString("content");
        String dateline = jsonObj.getString("dateline");
        String fenxiang_img = jsonObj.getString("fenxiang_img");
        String love = jsonObj.getString("love");
        String note = jsonObj.getString("note");
        String picture = jsonObj.getString("picture");
        String picture2 = jsonObj.getString("picture2");
        String s_pv = jsonObj.getString("s_pv");
        String sp_pv = jsonObj.getString("sp_pv");
        String translation = jsonObj.getString("translation");
        String tts = jsonObj.getString("tts");
        sentence.caption = caption;
        sentence.content = content;
        sentence.dateline = dateline;
        sentence.fenxiang_img = fenxiang_img;
        sentence.love = love;
        sentence.note = note;
        sentence.picture = picture;
        sentence.picture2 = picture2;
        sentence.s_pv = s_pv;
        sentence.sp_pv = sp_pv;
        sentence.translation = translation;
        sentence.tts = tts;
        
        // 解析关键字tags,它是一个JsonArray,遇到[]
        JSONArray jsonArray = jsonObj.getJSONArray("tags");
        // 新建Tag集合
        List<Sentence.Tag> tags = new ArrayList<Sentence.Tag>();
        for(int i=0;i<jsonArray.length();i++){
            Sentence.Tag tag = new Sentence.Tag();
            // jsonArray里的每一项都是JsonObject
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            tag.id = jsonObject.getInt("id");
            tag.name = jsonObject.getString("name");
            tags.add(tag);
        }
        sentence.tags = tags;
        
        return sentence;
    }

 使用这种方法解析JSON,看注释,没什么好多的,总结一句话就是:遇到{}用JSONObject,遇到[]用JSONArray,这样你就可以说你精通org.json解析JSON了。

  • 方式二:使用JsonReader解析JSON,JsonReader解析JSON有点类似PULL解析XML,主要的方法还是nextName()将游标后移。
    /**
     * Call requires API level 11 (current min is 8): new
     * android.util.JsonReader 通过org.json解析json
     * 
     * @param jsonStr
     *            json字符串
     * @throws Exception
     */
    @SuppressLint("NewApi")
    public static Sentence parseJsonByJsonReader(String jsonStr)
            throws Exception {
        // 新建Sentence
        Sentence sentence = new Sentence();
        // 新建Tag集合
        List<Sentence.Tag> tags = new ArrayList<Sentence.Tag>();
        JsonReader reader = new JsonReader(new StringReader(jsonStr));
        // 遇到{,开始解析对象
        reader.beginObject();
        while (reader.hasNext()) {
            String name = reader.nextName();
    
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值