How to use fastjson
This article introduces the knowledge of "how to use fastjson". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
JSONObject's method
Package name: com.alibaba.fastjson maven dependency:
The use of com.alibaba fastjson 1.2.7JSONObject converts strings to JSONObject objects String str = "{\" desc\ ":\" string in json format\ ",\" sex\ ":\" male\ "}"; JSONObject jo = JSONObject.parseObject (str)
JSONObject can convert extremely complex json objects, as can the following json statement, and obtain the corresponding values through the corresponding methods.
{"protocol": {"itemID": "9527", "companyID": "4399", "userID": "233"}," operateInfo ": {" cooptaskSceneDataPackets ": [{" taskID ":" 123", "runSceneID": "345"," dataTagCode ":" objection " "dataTagID": "", "dataTagName": "Yes", "dataTagBelongOrg": "}, {" taskID ":" 126 "," runSceneID ":" 345 "," dataTagCode ":"," dataTagID ":" "dataTagName": "Yes", "dataTagBelongOrg": ""}, {"taskID": "123"," runSceneID ":" 345", "dataTagCode": "unable", "dataTagID": "15th", "dataTagName": "Yes" "dataTagBelongOrg": "572"}, {"taskID": "124", "runSceneID": "235", "dataTagCode": "", "dataTagID": "", "dataTagName": "" "dataTagBelongOrg": ""}]} convert String objects to entity classes
Use JSONObject.parseObject (String text, Class clazz)
String str = "{\" itemID\ ":\" 9527\ ",\" companyID\ ":\" 4399\ ",\" userID\ ":\" 4399\ "}"; EntityProtocol entityProtocol = JSONObject.parseObject (str, EntityProtocol.class)
If the type does not match, the entity class with all attributes null is created.
Add or modify properties to the JSONObject object
Use the .put (key,value) method
If the key parameter is not in the original object, the key property is added and assigned to value in the original object.
If the key parameter exists, change the value of key to value
/ / JSONObject.put (String key,Object value); jo.put ("key", "value"); jo.put ("sex", "modify"); JSONObject.putAll (Map