JSON&&FASTJSON

Introducing JSON

Wikipedia took a passage from above: JSON ( JavaScript Object Notation, JS Object Notation) is a lightweight data interchange format. It is based on ECMAScript subset (European Computer Society norms established by js), using completely independent of the programming language of the text format to store and represent data. Simple and clear hierarchy make JSON an ideal data-interchange language. Easy to read and write, but also easy for machines to parse and generate, and effectively improve the efficiency of network transmission.

Summary: json data exchange format is a lightweight, easy to generate and parse the machine, and effectively improve network transmission efficiency.

JSON uses

JSON is mainly used for interactive data, especially in web services, along with Restful design style is more and more enterprises to adopt, JSON is also increasingly popular in the previous back-end data exchange.

JSON format

JSON with key / value data save format, JSON values ​​are what categories:

  1. Numbers (integer and floating point):{"age":20,"weight":55.2}
  2. String:{"name":"barry","addr":"China"}
  3. Array (integer array and object data):{"ages":[20,21], "persons":[{"age":20, "name":"barry"},{"age":21, "name":"harry"}]}
  4. Object:{"barry":{"age":20, "name":"barry"}}
  5. boolean: {"isMale":true}

One of the reasons few examples above JSON format to store almost covered, and if there is need for other formats can be freely combined, provided that they meet the requirements JSON format on the line, which is JOSN welcome it, more so convenient and powerful.

JSON parsing back-end

Ali open source JSON a powerful tool -FASTJSON, are interested can go and see the source code: https://github.com/alibaba/fastjson

  1. Cited package:

    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.57</version>
    </dependency>
    
  2. Data format conversion: FASTJSON provide a lot of useful data format conversion method, the object can easily be packaged and resolution, and transmission

    • Object:
    public class Person{
         private int age;
         private String name;
         // 省略setter,getter
     }
     
     // 对象转json字符串
     String personStr = JSON.toJSONString(person);
     // json字符串转对象
     Person person = JSON.parseObject(personStr, Person.class);
    
    1. set:
    // 定义集合map
    Map<String, Person> cacheDataMap = new HashMap<>();
    // map转为json字符串
    String cacheDataStr = JSONObject.toJSONString(cacheDataMap)// json字符串转map
    Map<String, Person> cacheDataMap = JSONObject.parseObject(cacheDataStr, new TypeReference<Map<String, Person>>(){});
    
Published 74 original articles · won praise 23 · views 40000 +

Guess you like

Origin blog.csdn.net/qxhly/article/details/89515959