object轉換jsonObject


{
    "msg_id": "36010000000000221589792383034",
    "device_id": "3601000000000022",
    "device_type":"ZDK",
    "event_time": "1589792383",
    "type": "DEVICE_REPORT",
    "data": {
        "subdev_id": "0101ec1bbdfffebb61c9",
        "group_sid": "0101ec1bbdfffebb61c9",
        "subdev_type": "0101ec1bbdfffebb61c9"
    }
}
 
當給你這一串東西的時候,后端應該怎么接受呢,不多說了,直接貼代碼
 
@PostMapping("/test")
public Object test(@RequestBody Object object) {
JSONObject jsonObject = JSONObject.fromObject(object);
System.out.println(jsonObject.getJSONObject("data").getString("subdev_id"));
return null;
}

重點在於第一句代碼,轉換
但是要引入一個jar包
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>

</dependency>
盡情玩轉Object跟json的轉換吧


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM