Android开发之解析不同json数据,Gson


public class JsonParse {
    public static JsonParse mInstance=null;
    private Context context;


    public JsonParse() {

    }

    public static JsonParse getmInstance() {
        if(mInstance==null){
            mInstance=new JsonParse();
        }
        return mInstance;
    }
/*对应json样例
*{
    "total": 1,
    "rows": [
        {
            "searchValue": null,
            "createBy": "admin",
            "createTime": "2021-05-06 15:40:17",
            "updateBy": "1",
            "updateTime": "2021-06-25 11:02:40",
            "remark": null,
            "params": {},
            "id": 25,
            "appType": "smart_city",
            "status": "1",
            "sort": 2,
            "advTitle": "首页轮播",
            "advImg": "/prod-api/profile/upload/image/2021/05/06/b9d9f081-8a76-41dc-8199-23bcb3a64fcc.png",
            "servModule": "新闻详情",
            "targetId": 28,
            "type": "2"
        }
    ],
    "code": 200,
    "msg": "查询成功"
}*/

    public List<BannerBean.RowsBean> getBannerList(String json){
        Gson gson=new Gson();
        Type type=new TypeToken<List<BannerBean.RowsBean>>(){}.getType();
        List<BannerBean.RowsBean> rowsBeanList=new ArrayList<>();
        try {
            JSONObject jsonObject=new JSONObject(json);
            JSONArray jsonArray=jsonObject.getJSONArray("rows");
            rowsBeanList=gson.fromJson(String.valueOf(jsonArray),type);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return rowsBeanList;
    }
/**json样例
{
    "total": 3,
    "rows": [
        {
            "searchValue": null,
            "createBy": null,
            "createTime": "2022-03-14 12:20:01",
            "updateBy": null,
            "updateTime": null,
            "remark": null,
            "params": {},
            "id": 22,
            "imgUrl": "/prod-api/profile/upload/image/2022/03/14/e4fadab0-2aeb-43c2-9a73-22b6a79e6de6.jpeg",
            "typeId": 10,
            "name": "早产宝贝救援计划",
            "author": "腾讯公益",
            "activityAt": "2018-03-30",
            "moneyTotal": 2920000,
            "moneyNow": 13476595,
            "description": "",
            "isRecommend": "0",
            "detailsList": null,
            "type": {
                "searchValue": null,
                "createBy": null,
                "createTime": null,
                "updateBy": null,
                "updateTime": null,
                "remark": null,
                "params": {},
                "id": 10,
                "name": "疾病救助",
                "sort": 1,
                "imgUrl": "/prod-api/profile/upload/image/2022/03/14/380a9465-d483-4a73-9f14-892db2e13b7a.png"
            },
            "donateCount": 123
        },
        {
            "searchValue": null,
            "createBy": null,
            "createTime": "2022-03-14 12:16:52",
            "updateBy": null,
            "updateTime": null,
            "remark": null,
            "params": {},
            "id": 21,
            "imgUrl": "/prod-api/profile/upload/image/2022/03/14/d2a743fe-d686-40af-a881-231901d17303.png",
            "typeId": 10,
            "name": "9958急救储备金计划",
            "author": "腾讯公益",
            "activityAt": "2018-03-23",
            "moneyTotal": 64320000,
            "moneyNow": 10205614,
            "description": "",
            "isRecommend": "0",
            "detailsList": null,
            "type": {
                "searchValue": null,
                "createBy": null,
                "createTime": null,
                "updateBy": null,
                "updateTime": null,
                "remark": null,
                "params": {},
                "id": 10,
                "name": "疾病救助",
                "sort": 1,
                "imgUrl": "/prod-api/profile/upload/image/2022/03/14/380a9465-d483-4a73-9f14-892db2e13b7a.png"
            },
            "donateCount": 25
        },
        {
            "searchValue": null,
            "createBy": null,
            "createTime": "2022-03-14 11:25:30",
            "updateBy": null,
            "updateTime": null,
            "remark": null,
            "params": {},
            "id": 11,
            "imgUrl": "/prod-api/profile/upload/image/2022/03/14/0a927ca2-142c-4b3b-8779-38965931213a.png",
            "typeId": 10,
            "name": "壹基金海洋天堂计划",
            "author": "腾讯公益",
            "activityAt": "2016-03-14",
            "moneyTotal": 13900000,
            "moneyNow": 14996309,
            "description": "",
            "isRecommend": "1",
            "detailsList": null,
            "type": {
                "searchValue": null,
                "createBy": null,
                "createTime": null,
                "updateBy": null,
                "updateTime": null,
                "remark": null,
                "params": {},
                "id": 10,
                "name": "疾病救助",
                "sort": 1,
                "imgUrl": "/prod-api/profile/upload/image/2022/03/14/380a9465-d483-4a73-9f14-892db2e13b7a.png"
            },
            "donateCount": 43
        }
    ],
    "code": 200,
    "msg": "查询成功"
}*/
   public List<GongyiListBean.RowsBean> getGongyiList(String json){
        Gson gson=new Gson();
        Type type=new TypeToken<List<GongyiListBean.RowsBean>>(){}.getType();
        List<GongyiListBean.RowsBean> rowsBeanList=new ArrayList<>();
        try {
            JSONObject jsonObject=new JSONObject(json);
            JSONArray jsonArray=jsonObject.getJSONArray("rows");
            rowsBeanList=gson.fromJson(String.valueOf(jsonArray),type);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return rowsBeanList;
    }
    public GongyiListBean.RowsBean.TypeBean getGongyiLeibie(String json,int position){
        Gson gson=new Gson();
        Type type=new TypeToken<GongyiListBean.RowsBean.TypeBean>(){}.getType();
        GongyiListBean.RowsBean.TypeBean rowsBeanList=new GongyiListBean.RowsBean.TypeBean();
        try {
            JSONObject jsonObject=new JSONObject(json);
            JSONArray jsonArray=jsonObject.getJSONArray("rows");
            JSONObject jsonObject1=jsonArray.getJSONObject(position);
            JSONObject jsonObject2=jsonObject1.getJSONObject("type");
            rowsBeanList=gson.fromJson(String.valueOf(jsonObject2),type);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return rowsBeanList;
    }
/**对应json样例
{
    "msg": "操作成功",
    "code": 200,
    "user": {
        "userId": 1111241,
        "userName": "lpw",
        "nickName": "大卫1",
        "email": "David@123.com",
        "phonenumber": "1235",
        "sex": "0",
        "avatar": "/profile/2020/10/26/27e7fd58-0972-4dbf-941c-590624e6a886.png",
        "idCard": "210113199808242137",
        "balance": 853.00,
        "score": 1000
    }
}**/
 public UserInforBean.UserBean getUserInfor(String json){
        Gson gson=new Gson();
        Type type=new TypeToken<UserInforBean.UserBean>(){}.getType();
        UserInforBean.UserBean rowsBeanList=new UserInforBean.UserBean();
        try {
            JSONObject jsonObject=new JSONObject(json);
            JSONObject jsonArray=jsonObject.getJSONObject("user");
            rowsBeanList=gson.fromJson(String.valueOf(jsonArray),type);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return rowsBeanList;
    }

}  

上方写了三种json语句解析,在图中都有样例,希望有帮助到你,如果帮助到你就

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值