web123456

Tips for using fastjson

favoritefastjson, because it only needs to rely on the JDK, all other environments do not need to rely on can be used. And it is very fast. Here I also use it to write out some insights and summarize for your correction.

Speaking of fastjson, we need to understand the concept of serialization: the object information is converted into information that can be transmitted and stored [Baidu Encyclopedia].

When we don't need a field we can use @JSONFiled(serialize=false) to unserialize it, by default both serialize and deserialize are true in the JSONFiled class.

Inside @JSONFiled() there are a lot of parameters that can be set, such as the type of the time @JSONFiled(format="yyyyMMdd HHmmss"), and you can also specify the name to be used when serializing.

  1. @Id
  2. @Column(name = "id")
  3. @JSONField(name = "ID")
  4. @GeneratedValue(strategy= )
  5. public int getId() {
  6. return id;
  7. }
For example, we specify the serialized name as "ID" above.

      

Another advantage of fastjson is that it integrates well with entity classes in java. We define the entity class. Then we return to the front-end use, there are some fields we do not need or front-end does not require the provision of . So we can use the filter to filter it out.

   SimplePropertyPreFilter filter = new SimplePropertyPreFilter(,"ID","contractContent","fixtureTime","contractContent","typeName","dealMark","transactionAmount");
   return  (new BaseModel(Constants.SUCCESS_CODE,fundLists2),filter);
The fields returned in this way are the prescribed fields defined inside our Filter.

When we output the relevant columns in order, we can add the annotation @JSONField(ordinal=0) to the output model class . as follows:

  1. @JSONField(ordinal = 0)
  2. private String code;
  3. @JSONField(ordinal = 1)
  4. private Map<String,String> myGroup;
  5. @JSONField(ordinal = 2)
  6. private Map<String,String> addGroup;

In addition we need to return relevant information when holding an object, it will be parsed incorrectly, for example, parsed into an object type such as $ref, we can do some processing in the parsing (added the red marked part), as follows:

   return (new BaseModel(Constants.SUCCESS_CODE,resultList),filter,<strong><span style="color:#ff0000;"></span></strong>);

There are also some less frequently used libraries that are actually quite powerful.JSONPath

  1. package ;
  2. import ;
  3. import ;
  4. import ;
  5. import ;
  6. public class JSONPathTest {
  7. public static void main(String[] args) {
  8. List<MyEntity> entities = new ArrayList<MyEntity>();
  9. (new MyEntity(1001, "ljw1001"));
  10. (new MyEntity(1002, "yan1002"));
  11. (new MyEntity(1003, "asw1003"));
  12. (new MyEntity(1004, null));
  13. /**
  14. * :: Use fastjson as a query, not sure how efficient this is compared to ID queries
  15. * [id in (1001,1003)] Filter elements in a collection by a specific ID
  16. * [0,2] Filter the elements in the set by subscript, return the elements with 0 and 2 in the subscript.
  17. * [0:2] Filter the elements in the set by subscripts, return the elements 0-2 in the subscripts.
  18. * $.name Returns all the names in MyEntity.
  19. *
  20. */
  21. List<MyEntity> result = (List<MyEntity>)(entities, "[id in (1001,1003)]");
  22. List<MyEntity> result1 = (List<MyEntity>)(entities, "[0,2]");
  23. List<MyEntity> result2 = (List<MyEntity>)(entities, "[0:2]");
  24. List<MyEntity> result3 = (List<MyEntity>)(entities, "$.name");
  25. MyEntity myEntity = (0);
  26. //(result3);
  27. MyEntity myEntity2 = new MyEntity(1005,"asd1005","String Object");
  28. String value = (String)(myEntity2, "$.value");
  29. (value);
  30. //whether there is a value element
  31. boolean isContain = (myEntity2, "$.value", ());
  32. (isContain);
  33. int size = (myEntity2, "$");
  34. (size);
  35. }
  36. }
  37. class MyEntity{
  38. private int id;
  39. private String name;
  40. private Object value;
  41. public MyEntity(){
  42. }
  43. public MyEntity(int id,String name){
  44. this.id = id;
  45. this.name = name;
  46. }
  47. public MyEntity(int id,String name,Object value){
  48. this(id,name);
  49. this.value = value;
  50. }
  51. @Override
  52. public String toString() {
  53. return "MyEntity [hljs-string">", name=" + name + "]";
  54. }
  55. //getter and setter
  56. }
Usage of ParseProcess:

  1. package ;
  2. import ;
  3. import ;
  4. import ;
  5. import ;
  6. import ;
  7. import ;
  8. public class VO {
  9. private int id;
  10. private Map<String, Object> attributes = new HashMap<String, Object>();
  11. public int getId() { return id; }
  12. public void setId(int id) { this.id = id;}
  13. public Map<String, Object> getAttributes() { return attributes;}
  14. public static void main(String[] args) {
  15. ExtraProcessor processor = new MyExtraProcessor();
  16. VO vo = ("{\"id\":123,\"value\":\"123456\"}", , processor);
  17. (());
  18. //value has been forced into an Integer type.
  19. (().get("value"));
  20. }
  21. }
  22. class MyExtraProcessor implements ExtraProcessor, ExtraTypeProvider {
  23. public void processExtra(Object object, String key, Object value) {
  24. VO vo = (VO) object;
  25. ().put(key, value);
  26. }
  27. public Type getExtraType(Object object, String key) {
  28. if ("value".equals(key)) {
  29. return int.class;
  30. }
  31. return null;
  32. }
  33. };

Extract collection objects inside JSON strings

Of course, we can also use fastJson to parse JSON strings, for example, we have the following JSON string.

  1. {
  2. "code": "00",
  3. "t": [
  4. {
  5. "chargeFree": 0,
  6. "date": "12-03 19:12",
  7. "showProdKeyName": "EUR/USD",
  8. "userImg": "http://116.31.94.164:8090/market-master-gateway/resources/images/upload",
  9. "verifyFlg": 0,
  10. "praiseCount": 0,
  11. "readCount": 0,
  12. "ifForward": 0,
  13. "points": 50,
  14. "ifSuccess": 0,
  15. "targetPrice": "1.0624"}
  16. ]
  17. }

We want to parse parsing t into an object, using the following:

  1. JSONObject jsonObject = (str);
  2. String data = ("t").toString();
  3. List<ViewPointEntity> lists = (data,);

Of course it's okay to omit the intermediate steps, which can be done in a better way below:

        List<ViewPointEntity> lists = (List<ViewPointEntity>)((str).get(t));

Extract json and encapsulate it directly into an object

For example, a json object is in the following format:

  1. {
  2. "ID": 515697,
  3. "chargeFree": 0,
  4. "commentCount": 0,
  5. "confidence": "30"
  6. }

You can perform string to object operation with the following code:

    ViewPointEntity viewpoint = (msg,);



You can convert the above t into a List, and then you can manipulate it.

Additionally we can make theChromeBrowser to load a JSON recognition plug-in project address: /gildas-lormeau/JSONView-for-Chrome, download and unzip, load the unzipped program to locate the WebContent on it.