博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JSON字符串 拼接与解析
阅读量:4634 次
发布时间:2019-06-09

本文共 1501 字,大约阅读时间需要 5 分钟。

常用方式:

json字符串拼接(目前使用过两种方式):

1.运用StringBuilder拼接
StringBuilder json = new StringBuilder();
json.append("{");
json.append(""uuid":" + """ + uuid + "",");
json.append(""phone":" + """ + list.get(i).get("DEST_NUMBER") + "",");
json.append(""tempTypeId":"6",");
json.append(""tempStatus":"0",");
json.append(""account":" + """ + list.get(i).get("DEST_NUMBER") + "",");
json.append(""amount":" + """ + Integer.parseInt(list.get(i).get("AMOUNT") + "") / 100 + "",");
json.append(""result":"0",");
json.append(""source":"JF",");
json.append(""first":"",");
json.append(""url":"",");
json.append(""remark":""");
json.append("}");
String jsonstr = json.toString();

2.运用JSONArray(JsonObject)

JSONObject jo = new JSONObject(); // 对象{}
JSONArray json = new JSONArray();// 数组[]
jo.put("id", Convert.convertNull(hm.get("cost_center_id")));
jo.put("Pid", Convert.convertNull(hm.get("parent_center_id")));
jo.put("name", Convert.convertNull(hm.get("cost_center_name")));
jo.put("isParent", Long.parseLong(Convert.convertNull(hm.get("count"))) > 0 ? true : false);
jo.put("levelId",Convert.convertNull(hm.get("level")));
String jsonObj = jo.toString();
json.put(jo);
String jsonArr = json.toString();

json解析:

1.List idList = (List) JSONUtil.deserialize(agreementIds);

2.运用JSONObject

注:

json.append("msg", "a1")
json.append("msg", "a2")
{"msg":["a1", "a2"]}
---数组追加

json.put("msg", "a1")

json.put("msg", "a2")
{"msg":"a2"}
---ojbect替换

这里一位女程序员的总结很好 :

转载于:https://www.cnblogs.com/workstation-liunianguowang/p/6772940.html

你可能感兴趣的文章
Bootstrap 栅格系统 理解与总结
查看>>
oracle的for和i++
查看>>
YML(2)yml 语法
查看>>
线段树专辑——pku 2886 Who Gets the Most Candies?
查看>>
求一个字符串中连续出现的次数最多的子串
查看>>
寒假作业pta3
查看>>
ubuntu使用记录
查看>>
面试题:查询连续出现的数字
查看>>
JS简单实现自定义右键菜单
查看>>
一个妹子图应用客户端源码
查看>>
day22_面向对象
查看>>
win10+Linux双系统安装及一些配置问题
查看>>
django-debug-toolbar使用指南
查看>>
2nd 四人小组项目的进一步分析
查看>>
http账户密码的截取
查看>>
LoadRunner中log的使用总结
查看>>
time 和 datetime 模块
查看>>
maven
查看>>
一.Timesten安装
查看>>
微软安全新闻聚焦-双周刊第三十四期
查看>>