当前位置:网站首页>JSON数据转换总结(VIP典藏版)
JSON数据转换总结(VIP典藏版)
2022-08-01 14:16:00 【托尼老师呀】
目录
一、测试数据
private static String getJsonData() { return "{\n" + " \"companyNo\": \"C001\",\n" + " \"supplyNo\": \"S001\",\n" + " \"orders\": [\n" + " {\n" + " \"orderNo\": \"aaa001\",\n" + " \"orderType\": \"order001\",\n" + " \"orderAmount\": \"100\",\n" + " \"creatorDate\": \"20020505\",\n" + " \"invoiceAmount\": \"500\",\n" + " \"openItemAmount\": \"700\"\n" + " },\n" + " {\n" + " \"orderNo\": \"ccc002\",\n" + " \"orderType\": \"order002\",\n" + " \"orderAmount\": \"600\",\n" + " \"creatorDate\": \"20010506\",\n" + " \"invoiceAmount\": \"123\",\n" + " \"openItemAmount\": \"456\"\n" + " }\n" + " ]\n" + "}"; }二、Str、JSONObject、JSONArray等互转
private static void json2json(String reqString) { reqString = "".equals(reqString) ? null : reqString; //string转object对象 JSONObject reqJson = JSONObject.parseObject(reqString); //获取对应字段 String companyNo = reqJson.getString("companyNo"); //JSONObject转JSONArray String orders = reqJson.getString("orders"); JSONArray jsonArray = JSONArray.parseArray(orders); //jsonObject-json互转 JSON json = reqJson.getObject("orders", JSON.class); String str = json.toJSONString(); JSONObject jsonObject = JSONObject.parseObject(str); JSONArray jsonArray2 = JSONArray.parseArray(str); JSONArray dealArray = dealArray(jsonArray); System.out.println(reqJson); }
三、数据遍历、不规则数据整理
//遍历,处理不规则数据 private static JSONArray dealArray(JSONArray array) { JSONArray array2 = new JSONArray(); for (int i = 0; i < array.size(); i++) { JSONObject json = array.getJSONObject(i); JSONObject jsonObject = new JSONObject(); for (String key : json.keySet()) { jsonObject.put(key, json.get(key).toString().trim()); } array2.add(jsonObject); } return array2; }四、JSON转XML
五、XML转JSON
边栏推荐
猜你喜欢

AD单片机九齐单片机NY8B062D SOP16九齐

Gradle series - Gradle tests, Gradle life cycle, settings.gradle description, Gradle tasks (based on Groovy documentation 4.0.4) day2-3

Batch replace tables in Word with pictures and save

gpio analog serial communication

openEuler 社区12位开发者荣获年度开源贡献之星

lua脚本关键

台积电认清了形势,新的建厂计划没有美国,中国芯片也得到重视
![[Binary Tree] Path Sum II](/img/ed/741b213f620f19975bdb479de015b1.png)
[Binary Tree] Path Sum II

1161. 最大层内元素和

Chat technology in live broadcast system (8): Architecture practice of IM message module in vivo live broadcast system
随机推荐
微服务原生案例搭建
HTB-Mirai
Multi-threaded cases - blocking queue
Typora报错:This beta version of Typora is expired
The problem that the column becomes indexed after pd groupby and the aggregation column has no column name
沃文特生物IPO过会:年营收4.8亿 养老基金是股东
NFV迈向云原生时代:Network Service Mesh项目介绍
Service Mesher Meetup 成都站:Service Mesh是下一代SDN吗?
牛客刷SQL--6
gpio analog serial communication
Performance Optimization - Animation Optimization Notes
论文笔记All about Eve: Execute-Verify Replication for Multi-Core Servers
超全!全国近90所大学考研报录比汇总!
DDL和DML的含义与区别「建议收藏」
【码蹄集新手村600题】判断一个数字是否为完全平方数
细读《阿里测试之道》
又拿三个大奖?!多力就是要让你吃的更营养更健康
魔众文档管理系统 v5.0.0
性能优化——动画优化笔记
长江欧拉生态创新中心成立,武汉数字经济再添坚实底座
