JSON usage summary, JSON usage Summary

Source: Internet
Author: User
Tags package json

JSON usage summary, JSON usage Summary

Key: value is stored in JSON. In fact, many of them are stored in the key: value format during programming. For example, map, java object (a property of an object has only one value), the key: value in the database corresponds to a data stored in it, redis is essentially key: value. So you will find that key: value is very useful.

1. convert any form of content into a string in the form of key: value (observed output), such as a json file.

{    "name": "Tom",    "age" : 12,    "book":{"1":"Math","2":"Chinese","3":"English"},    "interest" : ["football","basketball"],    "student" : {"name" :"Tom","age":22,"book":"String"}     }

After searching on the Internet, many people have written the method of converting the. json file into a string and then converting it to JSONObject. Currently, you can only find GSON and directly enter the location of the json file to directly output something similar to JSONObject (which has not been carefully studied). Other methods have not yet been found.

2. Convert string to JSONObject.

Package json; import java. awt. list; import java. util. arrayList; import com. alibaba. fastjson. JSON; import com. alibaba. fastjson. JSONArray; import com. alibaba. fastjson. JSONObject; public class TestJson {public static void main (String [] args) {// read the JSON file as a String. No good method has been found for direct conversion, you can search online. // GSON can be directly installed, but the GSON package is different from the package here. Util fr = new Util (); String str = fr. readFile ("src/json1.json"); // read the json file as a string. System. out. println (str. toString (); // convert the string to JSONObject. You can get anything through JSONObject. // You can also traverse JSONObject student1 = JSON. parseObject (str); String name = (String) student1.get ("name"); // obtain a String. String book = student1.getString ("book"); // you can convert string to JSONObject bookJson = JSON. parseObject (book); System. out. println (book); // The obtained JSONObject bookObj = student1.getJSONObject ("book"); String b1 = bookObj. getString ("1"); String b2 = bookObj. getString ("2"); String b3 = bookObj. getString ("3"); System. out. println (b1 + "," + b2 + "," + b3); String interest = student1.getString ("intere St "); JSONArray interestArr = student1.getJSONArray (" interest "); String arr1 = interestArr. getString (0); String arr2 = interestArr. getString (1); System. out. println (arr1 + "," + arr2); // ing. Directly. JSONArray inArr = student1.getObject ("interest", JSONArray. class); // if the value is an object, it can be directly mapped into an object. Student stu = student1.getObject ("student", Student. class); System. out. println (name); System. out. println (interest); System. out. println (stu. toString ());}}
package json;import java.util.List;public class Student {        public Student() {            }    public Student(String name, Integer age, String book) {        this.name = name;        this.age = age;        this.book = book;                }        public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public Integer getAge() {        return age;    }    public void setAge(Integer age) {        this.age = age;    }    public String getBook() {        return book;    }    public void setBook(String book) {        this.book = book;    }    private String name;    private Integer age;    private String book;        @Override    public String toString() {        return "Student [name=" + name + ", age=" + age + ", book=" + book                 + "]";    }    } 


Q: If the object Student contains List <String> interest, how can I map it? What is the value style in the corresponding json? If you have an address, please leave a message.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.