fastJson 笔记-简单应用

前端之家收集整理的这篇文章主要介绍了fastJson 笔记-简单应用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

之前写了半篇没保存,然后悲剧了。。。

这里只记录几个使用情景吧,记录从点滴,有基础的直接忽略本篇吧

1.解析简单(一维)的json数据

数据举例如下

/*
                 * rs: { "code"@H_403_11@:"E0001"@H_403_11@,//状态吗 "msg"@H_403_11@:"拒绝访问"@H_403_11@,* "mobileNumber"@H_403_11@:10@H_403_11@ //能够处理的号码总数 }
                 */

代码举例

String@H_403_11@ retureString = resquest.getResultString();
                JSONObject emayReturn = JSON@H_403_11@.parSEObject(retureString);
                String@H_403_11@ code = String@H_403_11@.valueOf(emayReturn.get("code"@H_403_11@));

转map解析代码举例如下

Map@H_403_11@<String@H_403_11@,Object> map@H_403_11@ = JSONObject.parSEObject(data);
int successCount = CommonUtils.parseInt(map@H_403_11@.get("successCount"@H_403_11@));
//int successCount = CommonUtils.get(map@H_403_11@,"successCount "@H_403_11@);
String@H_403_11@ code = map@H_403_11@.get

2.解析复杂的json数据

数据举例如下

/* data 数据格式如下
        {
            "successCount"@H_403_11@:1@H_403_11@,//成功数量@H_403_11@
            "errorlist"@H_403_11@:[                           //错误号码列表@H_403_11@
                {
                    "mobile"@H_403_11@:"15563736374"@H_403_11@,//手机号@H_403_11@
                    "code"@H_403_11@:"N0001"@H_403_11@              //状态码@H_403_11@
                },]
        }
         */

直接解析代码举例如下

JSONObject obj = JSON.parSEObject@H_403_11@(data);@H_403_11@
int successCount = CommonUtils.parseInt@H_403_11@(obj.get@H_403_11@("successCount"@H_403_11@));@H_403_11@
String code = obj.getJSONArray@H_403_11@("errorlist"@H_403_11@).getJSONObject@H_403_11@(0@H_403_11@).get@H_403_11@("code"@H_403_11@)

3.obj转json

List@H_403_11@<@H_403_11@Map@H_403_11@<@H_403_11@String@H_403_11@,Object>>@H_403_11@ errList =@H_403_11@ new@H_403_11@ ArrayList<@H_403_11@Map@H_403_11@<@H_403_11@String@H_403_11@,Object>>@H_403_11@();
        Map@H_403_11@<@H_403_11@String@H_403_11@,Object>@H_403_11@ errMap =@H_403_11@ new@H_403_11@ HashMap<@H_403_11@String@H_403_11@,Object>@H_403_11@();
        errMap.@H_403_11@put("message"@H_403_11@,"充值失败"@H_403_11@);
        errMap.@H_403_11@put("code"@H_403_11@,"99999"@H_403_11@);
        errMap.@H_403_11@put("mobile"@H_403_11@,"123456789"@H_403_11@);
        errList.@H_403_11@add(errMap);

        Map@H_403_11@<@H_403_11@String@H_403_11@,Object>@H_403_11@ params@H_403_11@ =@H_403_11@ new@H_403_11@ HashMap<@H_403_11@String@H_403_11@,Object>@H_403_11@();
        params@H_403_11@.@H_403_11@put("batchNo"@H_403_11@,"testApplyId1471435653"@H_403_11@);
        params@H_403_11@.@H_403_11@put("successCount"@H_403_11@,1@H_403_11@);
        params@H_403_11@.@H_403_11@put("failCount"@H_403_11@,0@H_403_11@);
        params@H_403_11@.@H_403_11@put("errorlist"@H_403_11@,errList);

        String@H_403_11@ data@H_403_11@ =@H_403_11@ JSON.@H_403_11@toJSONString(params@H_403_11@);
原文链接:https://www.f2er.com/json/289209.html

猜你在找的Json相关文章