博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【Java】提取JSON数值时遇到数组集合时使用的K-V方式转换
阅读量:7006 次
发布时间:2019-06-28

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

1.实体类转换方法

参照文章:http://www.cnblogs.com/dflmg/p/6933811.html

2.K-V方法(此方法比较笨,但是没有办法,我现在不知道有没有相关的简单API,只能自己手动拼出一个方法。import org.apache.commons.lang.StringUtils;)

String s = "{'request': {'enterpriseid': 55,'ownerCode': 'SunEee01','item': [{'itemName': '1熊孩子新疆无花果成品268g','itemType': 'ZC','barCode': '6924459400256','shelfLife ': 0, 'itemCode': 'xhzwhggcp_268'}, {'itemName': '2好孩子新疆无花果成品268g','itemType': 'ZC','barCode': '6924459400256','shelfLife ': 1, 'itemCode': 'xhzwhggcp_268'}]}}";        //String s = "{'request': {'enterpriseid': 55,'ownerCode': 'SunEee01','item': [{'itemName': '好孩子新疆无花果成品268g','itemType': 'ZC','barCode': '6924459400256','shelfLife ': 1, 'itemCode': 'xhzwhggcp_268'}]}}";        JSONObject jsonObject = JSONObject.fromObject(s);        //System.out.println(jsonObject);                String request = jsonObject.getString("request");        //System.out.println(request);                JSONObject vendorDO = JSONObject.fromObject(request);        //System.out.println(vendorDO);                String ent = vendorDO.getString("item");        //对象实体类的方法System.out.println(ent);        /*JSONArray ents = vendorDO.getJSONArray("item");        List
ss = (List
)JSONArray.toList(ents,Item.class); for(Item x:ss){
//如果item里面还有List对象作为属性就使用1的方法,参见http://www.cnblogs.com/dflmg/p/6933811.html System.out.println(x.getItemName()); }*/ String[] many = ent.split("\\},\\{"); if(many.length==1){ String one = many[0].substring(1,ent.length()-1); JSONObject joss = JSONObject.fromObject(one); String itemName = joss.getString("itemName"); System.out.println(itemName); }else{ for(int i=0;i

 

转载于:https://www.cnblogs.com/dflmg/p/7182976.html

你可能感兴趣的文章
解决兼容的方式
查看>>
畅通工程
查看>>
CentOS 安装MySQL(rpm)提示错误Header V3 DSA/SHA1 Signature
查看>>
Object-C runtime programming guide学习笔记
查看>>
Unity背包/商城物品逐个显示缓动效果-依次显示
查看>>
Spring c3p0连接池无法释放解决方案
查看>>
c#读取excel到dataset
查看>>
java使用Redis
查看>>
使用批处理设置、启动和停止服务
查看>>
测试报告 之 testNG + Velocity 编写自定义html测试报告
查看>>
openstack中文文档
查看>>
C++ 术语(更新中..)
查看>>
Developer Tools(开发工具)
查看>>
C#winform中ListView及ContextMenuStrip的使用
查看>>
WPF/Silverlight HierarchicalDataTemplate 模版的使用
查看>>
Leetcode | Longest Substring Without Repeating Characters
查看>>
滑动门
查看>>
中国剩余定理的应用
查看>>
先加载grid,再设置按钮状态
查看>>
Android WebView 自适应屏幕
查看>>