inhv的gravatar头像
inhv 2013-04-25 00:14:03

【无技术含量,但是很实用的 JSON 辅助java工具类】直接根据节点路径从 JSON 中 select 到最终的值。such as

通常我们实用 ajax 从服务端获取 json 数据,然后再 通过 JSONObject .get(key) 方法去获取到值。

例如:

{ "status": { "code": 1001, "msg": "正常" }, "result": { "data": { "is_end": false, "userId": "hello_kitty" } } }

应用场景: 我们现在要获取 userId 这个值。

通常我们的搞法是:

JSONObject resultJson = jo.get("result);

JSONObject dataJson = resultJson.get("data");

String userId = dataJson.getString("userId");

**OK, 现在终于获取到 userId 这个值了, BUT, 作为一名优秀的程序猿,不觉得这样太特码繁琐了么??

** 有没有一种简洁的办法,直接通过路径,一下子就获取到 userId 的值呢??

本工具 JSONUtils.java 就是为了解决这个问题:

String userId = (String) JSONUtils.select(jo, "result.data.userId"); //直接根据路径获取到最终的值


terryang编辑于2014-2-18 15:39:11


打赏

已有1人打赏

故事_sun的gravatar头像

文件名:JSONUtils.java,文件大小:1K 下载
最代码最近下载分享源代码列表最近下载
_M*  LV11 2021年5月12日
overflower  LV1 2020年6月17日
csy1209457788  LV3 2020年6月10日
lad213  LV2 2019年10月24日
caozongan  LV19 2019年9月24日
jubaoquanmess  LV1 2019年7月16日
abysswill  LV1 2019年7月5日
1784275983  LV2 2019年5月10日
lixingli1234  LV1 2019年4月17日
smilefighting526118  LV1 2019年3月22日
最代码最近浏览分享源代码列表最近浏览
hohhi  LV6 2023年4月19日
李韵龙  LV1 2022年1月12日
ls0000006  LV1 2022年1月10日
_M*  LV11 2021年5月12日
CJ_1998  LV4 2020年11月19日
overflower  LV1 2020年6月17日
csy1209457788  LV3 2020年6月10日
杨某某  LV7 2020年2月20日
jaflkjlkfdjl  LV6 2019年11月11日
lad213  LV2 2019年10月24日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友