String JSON<++> JSONObject.parseObject

丫的,天天用错对象,就不能长点脑子吗?

 System.out.println("message content:" + new String(message.getPayload()));
String json = JSON.toJSONString(String.valueOf(message));
System.out.println("111111111111   "+json + "   11111111111");
// "{\"sn\":\"04100017C76C5C9F\",\"appId\":\"OQDZpjTZ28H2\",\"owners\":\"5f6ff49412255f5c7829359e\",\"name\":\"润建研究院2楼会议室\",\"lonlat\":[108.27759600000006,22.85972900000002],\"interval\":22020,\"msgId\":\"6124d15bcfa96a0013572b0b\",\"deployFlag\":true,\"sensor\":{\"temperature\":28,\"lightLevel\":2,\"smoke\":false,\"cds\":false,\"humanDetection\":true,\"interval\":22020,\"battery\":94.8},\"deployTime\":\"2021-08-12T11:12:38+08:00\"}"
String strjson = stringUtil.trim(json, '\"');
String aa  =  org.apache.commons.lang3.StringEscapeUtils.unescapeJava(strjson);

Map mapObject = JSONObject.parseObject(aa, Map.class);
String json3 = JSON.toJSONString(mapObject);
SensoroCollectAlert s3 = JSON.parseObject(json3, SensoroCollectAlert.class);

String json2 = JSON.toJSONString(mapObject);
SensoroCollectAlert outboundMessage = JSON.parseObject(aa, SensoroCollectAlert.class);
SensoroCollectAlert sensoroCollectAlert = JSONObject.parseObject(json2, SensoroCollectAlert.class);


@Data
@ApiModel("升哲消防采集设备")
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class SensoroCollectAlert implements Serializable {
    private static long serialVersionUID = -6624730520403154632L;

//设备回传字段
    @ApiModelProperty(value = "采集设备回传编号", notes = "sn 与 系统deviceId对应")
    private String sn;

    @ApiModelProperty(value = "设备编码appId", notes = "appId 与 deviceSource对应")
    private String appId;

    @ApiModelProperty(value = "设备名称", notes = "name 与 collectName对应")
    private String name;

    @ApiModelProperty(value = "是否已部署", notes = "是否已部署 true -> 已部署;false -> 未部署")
    private Boolean deployFlag;

    @ApiModelProperty(value = "设备上报周期", notes = "interval 回传周期")
    private Long interval;

    @ApiModelProperty(value = "经纬度", notes = "lonlat :需要分开 lat 和 lon")
    private JSONArray lonlat;  //  "lonlat": [108.27759600000006,22.85972900000002],

    @ApiModelProperty(value = "经度", notes = "lon")
    private String lon;

    @ApiModelProperty(value = "纬度", notes = "lat")
    private String lat;

    @ApiModelProperty(value = "报警详情", notes = "sensor")
    private SensoroSmokeCell sensor;

}
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSON;
// EquipmentCollectAlert equipmentCollectAlert
String json = JSON.toJSONString(equipmentCollectAlert); 
String aa ="{\"deviceSource\":\"a-1\",\"deviceIP\":\"312.212.8\",\"simCardNo\":\"18856785678\",\"deviceAction\":\"手动报警模拟\",\"deviceName\":\"设备11\",\"deviceId\":6666,\"deviceType\":0,\"deviceStatus\":2,\"alarmName\":\"手动\",\"deviceAddress\":null,\"msgTime\":\"2020-09-16T11:59:29.335347+08:00\"}";
       
   equipmentCollectAlert = JSONObject.parseObject(aa, EquipmentCollectAlert.class);

List<Map<String, String>> listObjectSecs = JSONArray.parseObject(jsonStr, List.class);

   @Override
    public List<BuildingFloor> findBuildingFloors(BuildingFloor buildingFloor) {
        LambdaQueryWrapper<BuildingFloor> queryWrapper = getBuildingFloorLambdaQueryWrapper(buildingFloor);
        List<BuildingFloor> buildingFloorList = this.baseMapper.selectList(queryWrapper);
        List<BuildingFloor> buildingFloorList2 = buildingFloorList.stream()
                .map(floor -> {
                    String jsonCoordinate = null;
                    String jsonStr = floor.getFloorModelCoordinate();
                    if (!ObjectUtils.isEmpty(jsonStr)) {
                        List<Map<String, String>> listObjectSecs = JSONArray.parseObject(jsonStr, List.class);
                        List<Map<String, String>> listObjectSecs2 = listObjectSecs.stream()
                                .map(listObjectSec -> {
                                    if (!ObjectUtils.isEmpty(listObjectSec.get("collectId"))) {
                                        Long collectId = Long.valueOf(String.valueOf(listObjectSec.get("collectId")));
                                        EquipmentCollect collect = this.baseMapper.selectCollectById(collectId);
                                        Integer isNormal = collect.getIsNormal();
                                        listObjectSec.put("isNormal", String.valueOf(isNormal));
                                    }
                                    return listObjectSec;
                                }).collect(Collectors.toList());
                        jsonCoordinate = JSON.toJSONString(listObjectSecs2);
                    }
                    floor.setFloorModelCoordinate(jsonCoordinate);
                    return floor;
                }).collect(Collectors.toList());
        return buildingFloorList2;
    }
在这里插入代码片
String strArr = "[{\"0\":\"zhangsan\",\"1\":\"lisi\",\"2\":\"wangwu\",\"3\":\"maliu\"}," +
         "{\"00\":\"zhangsan\",\"11\":\"lisi\",\"22\":\"wangwu\",\"33\":\"maliu\"}]";
 //第一种方式
 List<Map<String,String>> listObjectFir = (List<Map<String,String>>) JSONArray.parse(strArr);
 System.out.println("利用JSONArray中的parse方法来解析json数组字符串");
 for(Map<String,String> mapList : listObjectFir){
     for (Map.Entry entry : mapList.entrySet()){
        System.out.println( entry.getKey()  + "  " +entry.getValue());
     }
 }
 //第二种方式
 List<Map<String,String>> listObjectSec = JSONArray.parseObject(strArr,List.class);
 System.out.println("利用JSONArray中的parseObject方法并指定返回类型来解析json数组字符串");
 for(Map<String,String> mapList : listObjectSec){
     for (Map.Entry entry : mapList.entrySet()){
         System.out.println( entry.getKey()  + "  " +entry.getValue());
     }
 }
 //第三种方式
 JSONArray listObjectThir = JSONArray.parseArray(strArr);
 System.out.println("利用JSONArray中的parseArray方法来解析json数组字符串");
 for(Object mapList : listObjectThir){
     for (Object entry : ((Map)mapList).entrySet()){
         System.out.println(((Map.Entry)entry).getKey()  + "  " +((Map.Entry)entry).getValue());
     }
 }
 //第四种方式
 List listObjectFour = JSONArray.parseArray(strArr,Map.class);
 System.out.println("利用JSONArray中的parseArray方法并指定返回类型来解析json数组字符串");
 for(Object mapList : listObjectFour){
     for (Object entry : ((Map)mapList).entrySet()){
         System.out.println(((Map.Entry)entry).getKey()  + "  " +((Map.Entry)entry).getValue());
     }
 }
 //第五种方式
 JSONArray listObjectFifth = JSONObject.parseArray(strArr);
 System.out.println("利用JSONObject中的parseArray方法来解析json数组字符串");
 for(Object mapList : listObjectFifth){
     for (Object entry : ((Map)mapList).entrySet()){
         System.out.println(((Map.Entry)entry).getKey()  + "  " +((Map.Entry)entry).getValue());
     }
 }
 //第六种方式
 List listObjectSix = JSONObject.parseArray(strArr,Map.class);
 System.out.println("利用JSONObject中的parseArray方法并指定返回类型来解析json数组字符串");
 for(Object mapList : listObjectSix){
     for (Object entry : ((Map)mapList).entrySet()){
         System.out.println(((Map.Entry)entry).getKey()  + "  " +((Map.Entry)entry).getValue());
     }
 }
 //第七种方式
 JSONArray listObjectSeven = JSON.parseArray(strArr);
 System.out.println("利用JSON中的parseArray方法来解析json数组字符串");
 for(Object mapList : listObjectSeven){
     for (Object entry : ((Map)mapList).entrySet()){
         System.out.println(((Map.Entry)entry).getKey()  + "  " +((Map.Entry)entry).getValue());
     }
 }
 //第八种方式
 List listObjectEigh = JSONObject.parseArray(strArr,Map.class);
 System.out.println("利用JSON中的parseArray方法并指定返回类型来解析json数组字符串");
 for(Object mapList : listObjectEigh){
     for (Object entry : ((Map)mapList).entrySet()){
         System.out.println(((Map.Entry)entry).getKey()  + "  " +((Map.Entry)entry).getValue());
     }
 }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值