Json data parsing

Source: Internet
Author: User

Json data parsing
??

Json data, which can be opened online or downloaded using bejson

What is JSON:

JSON is JavaScript Object Natation. It is a lightweight data exchange format. Like XML, JSON is a widely used client-server interaction solution.

Let's take a look at the code for a clearer understanding and understanding.

 

Java code
  1. Public class Address {private String street; private String city; private String country; public Address () {super ();} public Address (String street, String city, String country) {super (); this. street = street; this. city = city; this. country = country;} public String getStreet () {return street;} public void setStreet (String street) {this. street = street;} public String getCity () {return city;} public void setCity (String city) {this. city = city;} public String getCountry () {return country;} public void setCountry (String country) {this. country = country; }@ Override public String toString () {return Address [street = + street +, city = + city +, country = + country +];}
    public class Address {private String street;private String city;private String country;public Address() {super();}public Address(String street, String city, String country) {super();this.street = street;this.city = city;this.country = country;}public String getStreet() {return street;}public void setStreet(String street) {this.street = street;}public String getCity() {return city;}public void setCity(String city) {this.city = city;}public String getCountry() {return country;}public void setCountry(String country) {this.country = country;}@Overridepublic String toString() {return Address [street= + street + , city= + city + , country=+ country + ];}}

     

    Java code
    1. Public class Person {private String name; private int age; private boolean male; private Address address Address; public Person () {super ();} public Person (String name, int age, boolean male, Address address) {super (); this. name = name; this. age = age; this. male = male; this. address = address;} public String getName () {return name;} public void setName (String name) {this. name = name;} public int getAge () {return age;} public void setAge (int age) {this. age = age;} public boolean isMale () {return male;} public void setMale (boolean male) {this. male = male;} public Address getAddress () {return address;} public void setAddress (Address address Address) {this. address = address ;}@ Override public String toString () {return Person [name = + name +, age = + age +, male = + male +, address = + address +] ;}}
      public class Person {private String name;private int age;private boolean male;private Address address;public Person() {super();}public Person(String name, int age, boolean male, Address address) {super();this.name = name;this.age = age;this.male = male;this.address = address;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public boolean isMale() {return male;}public void setMale(boolean male) {this.male = male;}public Address getAddress() {return address;}public void setAddress(Address address) {this.address = address;}@Overridepublic String toString() {return Person [name= + name + , age= + age + , male= + male+ , address= + address + ];}}

      The specific Parsing Code is as follows:

       

      Java code
      1. JSONObject jsonObject = new JSONObject (json); String name = jsonObject. getString (name); int age = jsonObject. getInt (age); boolean male = jsonObject. getBoolean (male); JSONObject addressJSON = jsonObject. getJSONObject (address); String street = addressJSON. getString (street); String city = addressJSON. getString (city); String country = addressJSON. getString (country); Address address = new Address (street, city, country); Person person = new Person (name, age, male, address); System. out. println (person );
        JSONObject jsonObject = new JSONObject(json);String name = jsonObject.getString(name);int age = jsonObject.getInt(age);boolean male = jsonObject.getBoolean(male);JSONObject addressJSON = jsonObject.getJSONObject(address);String street = addressJSON.getString(street);String city = addressJSON.getString(city);String country = addressJSON.getString(country);Address address = new Address(street, city, country);Person person = new Person(name, age, male, address);System.out.println(person);

        LogCat output is Person [name = coolxing, age = 24, male = true, address = Address [street = huiLongGuan, city = beijing, country = china], the JSON object has been correctly parsed.

         

        2. parse JSON objects using the gson library. assume that the JSON data with resolution is json = {ame: coolxing, age = 24, male: true, address: {street: huiLongGuan, city: eijing, country: china }}, first, go to http://code.google.com/p/google-gson/to download the jarpackage and add it to the project. the specific Parsing Code is as follows:

         

        Java code
        1. Gson gson = new Gson (); Person person = gson. fromJson (json, Person. class); System. out. println (person );
          Gson gson = new Gson();Person person = gson.fromJson(json, Person.class);System.out.println(person);

          LogCat output is Person [name = coolxing, age = 24, male = true, address = Address [street = huiLongGuan, city = beijing, country = china], it indicates that the JSON object has been correctly parsed. Is it very easy?

           

          3. use the built-in org. the json package parses the JSON array. assume that the JSON data to be parsed is json = [{ame: coolxing, age = 24, male: true, address: {street: huiLongGuan, city: eijing, country: china }}, {ame: min, age = 20, male: false, address: {street: heiShiJiao, city: daLian, country: china}]. The parsing code is as follows:

           

          Java code
          1. List Persons = new ArrayList (); JSONArray jsonArray = new JSONArray (json); for (int I = 0; I <jsonArray. length (); I ++) {JSONObject jsonObject = jsonArray. getJSONObject (I); String name = jsonObject. getString (name); int age = jsonObject. getInt (age); boolean male = jsonObject. getBoolean (male); JSONObject addressJSON = jsonObject. getJSONObject (address); String street = addressJSON. getString (street); String city = addressJSON. getString (city); String country = addressJSON. getString (country); Address address = new Address (street, city, country); Person person = new Person (name, age, male, address); persons. add (person);} System. out. println (persons );
            List
                         
                           persons = new ArrayList
                          
                           ();JSONArray jsonArray = new JSONArray(json);for(int i = 0; i < jsonArray.length(); i++) {JSONObject jsonObject = jsonArray.getJSONObject(i);String name = jsonObject.getString(name);int age = jsonObject.getInt(age);boolean male = jsonObject.getBoolean(male);JSONObject addressJSON = jsonObject.getJSONObject(address);String street = addressJSON.getString(street);String city = addressJSON.getString(city);String country = addressJSON.getString(country);Address address = new Address(street, city, country);Person person = new Person(name, age, male, address);persons.add(person);}System.out.println(persons);
                          
                         

            LogCat output is [Person [name = coolxing, age = 24, male = true, address = Address [street = huiLongGuan, city = beijing, country = china], person [name = min, age = 20, male = false, address = Address [street = heiShiJiao, city = daLian, country = china], the JSON array has been correctly parsed.


            4. Use the gson library to parse the JSON array. The JSON data to be parsed is the same as the preceding one. The Code is as follows:

             

            Java code
            1. Gson gson = new Gson (); Type listType = new TypeToken > () {}. GetType (); List Persons = gson. fromJson (json, listType );
              Gson gson = new Gson();Type listType = new TypeToken
                               
                                >(){}.getType();List
                                
                                  persons = gson.fromJson(json, listType);
                                
                               
              LogCat output is [Person [name = coolxing, age = 24, male = true, address = Address [street = huiLongGuan, city = beijing, country = china], person [name = min, age = 20, male = false, address = Address [street = heiShiJiao, city = daLian, country = china], the JSON array has been correctly parsed. new TypeToken > () {}. GetType (); this Code creates an anonymous subclass object of TypeToken and calls the getType () method of the object.
              Org. there are many other useful APIs in the json package and gson library. You can view the documentation when necessary. for example, you may need to generate json data through a java object or java Collection or array and upload it to the server. Of course, you can construct a json string yourself, but it will be very troublesome. at this time, you can use the related APIs to conveniently complete this task.

               

Related Article

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.