一 JSONArray 添加 JSONObject
1 语法:
JSONArray.add(JSONObject);
2 格式:
[{
"key": "value",
"key": "value",
...
}]
举例:
JSONArray array = new JSONArray();
//从数据库获取packageOrderList
List<packageOrderDto> packageOrderList = packageOrderMybatis.getPackageOrderList(page,size,keyword);
for(packageOrderDto dto: packageOrderList ){
JSONObject obj = new JSONObject();
obj.put("orderId", dto.getOrderId());
obj.put("packageId", dto.getPackageId);
obj.put("startTime", dto.getStartTime());
obj.put("packageName",dto.getPackageName);
obj.put("endTime",dto.getEndTime());
obj.put("userId",dto.getUserId());
array.add(obj);
}
System.out.println(array);
结果:
"packageOrderList": [
{
"orderId": "3599192499316",
"packageId": "9dcf693021904ff9939d5",
"startTime": "2019-03-11 00:00:00",
"packageName": "云存储",
"endTime": "2019-04-30 00:00:00",
"userId": 152XXXXXXXX
}
]
二 JSONObject 添加 JSONArray
1 语法:
JSONObject.put(JSONArray);
2 格式:
"key": {
"key":[
{“key”:value},
{“key”:value},
...
]
}
举例:
JSONObject cameraObj= new JSONObject();
JSONArray array = new JSONArray();
String macType ="HMC-1" ;
String macId = "200059441003";
PackageOrderDto packageDto = new PackageOrderDto();
packageDto.setPackageId("001");
packageDto.setOrderId("套餐001");
PackageOrderDto packageDto2 = new PackageOrderDto();
packageDto2.setPackageId("002");
packageDto2.setOrderId("套餐002");
List<PackageOrderDto> packageOrderDtoList = new ArrayList<>();
packageOrderDtoList.add(packageDto);
packageOrderDtoList.add(packageDto2);
for(PackageOrderDto dto: packageOrderDtoList ){
JSONObject obj = new JSONObject();
obj.put("packageId", dto.getPackageId());
obj.put("orderId",dto.getOrderId());
array.add(obj);
}
cameraObj.put("macType",macType);
cameraObj.put("macId", macId);
cameraObj.put("packageOrderList",array);
System.out.println(cameraObj);
结果:
{
"macType": "HMC-1",
"macId": "200059441003",
"packageOrderList": [{
"packageId": "001",
"orderId": "套餐001"
}, {
"packageId": "002",
"orderId": "套餐002"
}]
}
今天的文章jsonarray的用法_jsonarray转string数组「建议收藏」分享到此就结束了,感谢您的阅读。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:http://bianchenghao.cn/74827.html