1, What is JSON
JSON (JavaScript Object Notation) is a lightweight data exchange format.
Easy to read and write. It is also easy to machine parse and generate.
It is based on JavaScript programming language, a subset of standard ecma-262 3rd Edition - December 1999.
JSON adopts a text format completely independent of the language, but it also uses habits similar to the C language family (including C, C++, C#, Java, JavaScript, Perl, Python, etc.). These features make JSON an ideal data exchange language.
2, JSON object definition and basic usage
In the standard json format, json objects are enclosed in parentheses. The attribute in the object, that is, the key of json, is a string, so you must use double quotation marks. Each group of keys is separated by commas.
1) Grammar rules
- Array s are represented by square brackets ("[]").
- The object (0bject) is represented by braces ("{}").
- name/value pairs are combined into arrays and objects.
- The name is enclosed in double quotation marks, and the value includes string, numeric value, Boolean value, null, object and array.
- Parallel data are separated by commas (",").
2) Definition of JSON
//Json definition format: var jsons = { "key1":"abc", // String type "key2":1234, // Number "key3":[1234,"21341","53"], // array "key4":{ // json type "key4_1" : 12, "key4_2" : "kkk" }, "key5":[{ // json array "key5_1_1" : 12, "key5_1_2" : "abc" },{ "key5_2_1" : 41, "key5_2_2" : "bbj" }] };
3) Access to JSON objects
json object, as the name suggests, you know it is an object. The key inside is the attribute of the object. To access the attributes of an object, we only need to use the method of [object name. Attribute name].
<script type="text/javascript"> // Definition of json var jsons = { "key1":"abc", // String type "key2":1234, // Number "key3":[1234,"21341","53"], // array "key4":{ // json type "key4_1" : 12, "key4_2" : "kkk" }, "key5":[{ // json array "key5_1_1" : 12, "key5_1_2" : "abc" },{ "key5_2_1" : 41, "key5_2_2" : "bbj" }] }; // Accessing properties of json ""Play" Java series alert(jsons.key1); // "abc" // Accessing array properties of json alert(jsons.key3[1]); // "21341" // Accessing json properties of json alert(jsons.key4.key4_1);//12 // Access json array of json alert(jsons.key5[0].key5_1_2);//"abc" </script>
3, Two common methods in JSON.
Conversion of JSON object and string object:
- JSON.stringify( json ); This method can convert a JSON object into a JSON string
- JSON.parse( jsonString ); This method can convert a json string into a json pair
<script type="text/javascript"> // A json object var obj = { "a" : 12, "c" : "str" }; // Convert json object to string object var objStr = JSON.stringify(obj); // alert(objStr); // Convert the string of json object into json object var jsonObj = JSON.parse(objStr); alert(jsonObj); </script>
4, Use of JSON in java (emphasis)
To use json and java, we need to use a third-party package. It's gson jar
Gson is a Java class library provided by Google to map between Java objects and JSON data. You can turn a JSON string into a Java object, or vice versa.
1) json operation in java. There are three common situations.
- Conversion of java objects and json
- Conversion of java object list collection and json
- Transformation of map object and json
//Make a test class for java objects public class GsonTest { static class Person { private int age; private String name; public Person() { // TODO Auto-generated constructor stub } public Person(int age, String name) { this.age = age; this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } ""Play" Java series public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Person [age=" + age + ", name=" + name + "]"; } }
1. Conversion of java objects and json
public static void main(String[] args) { // json operation, you must first create a new gson object. Gson gson = new Gson(); // java object -- json Person person = new Person(12, "wzg168"); // Convert object to json string String personjson = gson.toJson(person); // Convert json strings into java objects Person p = gson.fromJson(personjson, Person.class);
2. Conversion of java object list collection and json
// 2. Conversion of java object list collection and json List<Person> list = new ArrayList<Person>(); for (int i = 0; i < 3; i++) { list.add(new Person(10 * i, "name-" + i)); } //Convert the List object to a json string String jsonListString = gson.toJson(list); // Convert json array to List object // We can use anonymous inner classes List<Person> ps = gson.fromJson(jsonListString, new TypeToken<List<Person>>() { }.getType());
3. Transformation of map object and json
// 3. Transformation of map object and json Map<String, Person> mapPerson = new HashMap<String, GsonTest.Person>(); // Add person to map mapPerson.put("p1", new Person(1, "person-1")); mapPerson.put("p2", new Person(2, "person-2")); // Convert map to json object String jsonMapString = gson.toJson(mapPerson); System.out.println(jsonMapString); // By using anonymous inner classes Map<String, Person> map = gson.fromJson(jsonMapString,new TypeToken<HashMap<String, Person>>() {}.getType()); }