Java将String转换成json对象或json数组

2021-09-09 00:00:00 对象 数组 转换成

这里的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'," +
                "       '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 = jsonArray.getJSONObject(i);
            SmartProejctEquipmentMap myclass = JSONObject.parseObject(object.toJSONString() , SmartProejctEquipmentMap.class);// 将string类型直接封装成对象
            System.out.println(myclass.toString());
        }

《Java将String转换成json对象或json数组》

    原文作者:我是王小贱
    原文地址: https://blog.csdn.net/qq_34377273/article/details/105574465
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。

相关文章