Java将String转换成json对象或json数组
这⾥的SmartProejctEquipmentMap 是我⾃定的⼀个实体类,可以⾃⼰定义转换。
注意:json字符串中键的名称要和实体类⼀致。
@Test
public void TestJsonObject() {
String datajson = "" +
"[" +
" {" +
" 'projectId':'00a8de32-422c-4b8d-b945-d9cda7f30a31'," +
" 'projectName':'年产⽚式元器件7.2亿个、⾼分⼦固体电容器3.96亿个等项⽬(标准⼚房、连廊A,甲类仓库,办公楼,宿舍,值班门卫,垃圾房)'," + " 'iconType':'yc'," +
" 'eqpType':'yc'," +
" 'sn':'SZWJJJ8MQX20200403134027'," +
" 'name':'测试第三⽅01'," +
" 'wkt':'测试第三⽅01'," +
" 'sn':'POINT(120.698299 31.272362)'" +
" }," +
" {" +
" 'projectId':'00a8de32-422c-4b8d-b945-d9cda7f30a31'," +
" 'projectName':'测试项⽬编号名称'," +
" 'iconType':'yc'," +json转换对象
" 'eqpType':'yc'," +
" 'sn':'FDASDFAFDAFYF89AS6F897DD'," +
" 'name':'扬尘设备001'," +
" 'wkt':'扬尘设备001'," +
" 'sn':'POINT(120.698299 31.272362)'" +
" }" +
"]";
JSONArray jsonArray = JSONArray.parseArray(datajson);
for(int i=0; i<jsonArray.size(); i++) {
JSONObject object = JSONObject(i);
SmartProejctEquipmentMap myclass = JSONObject.JSONString() , SmartProejctEquipmentMap.class);// 将string类型直接封装成对象 System.out.String());
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论