Android中的Json解析,Gson、FastJson、JSONObject三种解析方式

JSON数据

接口测试:https://gank.io/api
在线解析:https://www.json.cn/

JSONObject.toJSONString(list)

这个可以生产json字符串

GsonFormat

主要用于使用Gson库将JSONObject格式的String 解析成实体,该插件可以加快开发进度,使用非常方便,效率高。

插件地址:
https://plugins.jetbrains.com/plugin/7654-gsonformat
介绍
https://www.cnblogs.com/1024zy/p/6370305.html

Gson的简单使用

https://github.com/google/gson

//https://github.com/google/gson
implementation 'com.google.code.gson:gson:2.8.5'

    // 使用new方法
    Gson gson = new Gson();
    
    // toJson 将bean对象转换为json字符串
    String jsonStr = gson.toJson(user, User.class);
    
    // fromJson 将json字符串转为bean对象
    Student user= gson.fromJson(jsonStr, User.class);
    
    // **序列化List**
    String jsonStr2 = gson.toJson(list);
    
    // **反序列化成List时需要使用到TypeToken getType()**
    List<User> retList = gson.fromJson(jsonStr2,new TypeToken<List<User>>(){}.getType());

FastJson的简单使用

https://github.com/alibaba/fastjson

//https://github.com/alibaba/fastjson
implementation 'com.alibaba:fastjson:1.2.44'
[
        {
            "coverImg": "http://img5.mtime.cn/mg/2018/07/22/100134.53759038_120X90X4.jpg",
            "hightUrl": "http://vfx.mtime.cn/Video/2018/07/22/mp4/180722100205713536.mp4",
            "id": 71318,
            "movieId": 213190,
            "movieName": "《哥斯拉:怪兽之王》中字预告",
            "rating": -1,
            "summary": "大怪兽集结!小11成焦点人物",
            "type": [
                "动作",
                "冒险",
                "科幻"
            ],
            "url": "http://vfx.mtime.cn/Video/2018/07/22/mp4/180722100205713536.mp4",
            "videoLength": 146,
            "videoTitle": "哥斯拉:怪兽之王 中字预告1"
        },
        {
            "coverImg": "http://img5.mtime.cn/mg/2018/07/22/041641.10540075_120X90X4.jpg",
            "hightUrl": "http://vfx.mtime.cn/Video/2018/07/22/mp4/180722041607197988.mp4",
            "id": 71311,
            "movieId": 87876,
            "movieName": "《雷霆沙赞》SDCC预告片",
            "rating": -1,
            "summary": "沙雕英雄身体里住了个孩子",
            "type": [
                "动作",
                "奇幻",
                "科幻"
            ],
            "url": "http://vfx.mtime.cn/Video/2018/07/22/mp4/180722041607197988.mp4",
            "videoLength": 173,
            "videoTitle": "雷霆沙赞 SDCC预告片"
        }]

  • 1.Movie.java 对上面JSON数组中的一项进行GsonFormat
  • 2.Json数组使用List movies = JSON.parseArray(json, Movie.class);
  • 3.List list = JSON.parseArray(jsonString);//如果是一个普通的List,里面没有Bean对象,则也可以这样解析。
  • 4.json对象的话,DataInfo dataInfo = JSON.parseObject(json, DataInfo.class);
  • JSON.toJSON(user)可以序列化

JSON的手动解析

  • 手动解析JSON对象和JSON数组
    fun test7() {
        val json1 = "{\"age\":12,\"name\":\"jack\"}"
        //手动解析json
        val jsonObject = JSONObject(json1)
        val name = jsonObject.optString("name")
        val age = jsonObject.optInt("age")
        // E/MainActivity: name=jack,age=12
        Log.e(TAG, "name=$name,age=$age")



        val json2 = "[{\"age\":10,\"name\":\"bob\"},{\"age\":12,\"name\":\"jack\"}]"
        //手动解析json数组
        val jsonArray = JSONArray(json2)
        repeat(jsonArray.length()){
            val jsonObject = jsonArray.getJSONObject(it)
            val name = jsonObject.optString("name")
            val age = jsonObject.optInt("age")
            // E/MainActivity: name=bob,age=10
            // E/MainActivity: name=jack,age=12
            Log.e(TAG, "name=$name,age=$age")
        }
    }

  • getInt和optInt的区别
    getInt,当没有找到时,会抛出异常
    optInt,没有找到时,不会抛出异常,返回默认值0
Caused by: org.json.JSONException: No value for age2
        at org.json.JSONObject.get(JSONObject.java:399)
        at org.json.JSONObject.getInt(JSONObject.java:488)
  • 或者类型不匹配时,使用get也会抛出异常
     Caused by: org.json.JSONException: Value jack at name of type java.lang.String cannot be converted to int
        at org.json.JSON.typeMismatch(JSON.java:101)
        at org.json.JSONObject.getInt(JSONObject.java:491)
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值