Android parses JSON data using class objects

Source: Internet
Author: User

1. parse data using the Button of the main Activity

MainActivity String Data = "{\" name \ ": \" Xiaozhang \ ", \" age \ ": 25, \" sex \ ": \" male \"}; {\ "name \": \ "Plum \", \ "age \": 41, \ "sex \": \ "female \"}; "+" {\ "name \": \ "Qian \", \ "age \": 15, \ "sex \": \ "female \"} "= (Button)

2. Business class for parsing json data

Gson gson = // before the split, it is a whole "{\" name \ ": \" John \ ", \" age \ ": 25, \" sex \": \ "male \" };{\ "name \": \ "Plum \", \ "age \": 41, \ "sex \": \ "female \"};"
    
= JsonData. split (";" (I = 0; I <users. length; I ++ User user = gson. fromJson (users [I], User. + "," + user. getAge () + "," +
The above method can also be written as follows:
       



However, it is emphasized that the Data in the main Activity isFormat
"{\" Name \ ": \" John \ ", \" age \ ": 25, \" sex \": \ "male \" }{\ "name \": \ "Plum \", \ "age \": 41, \ "sex \": \ "female \" }{\ "name \": \ "Qian \", \ "age \": 15, \ "sex \": \ "female \"}"
"\" Name \ ": \" John \ ", \" age \ ": 25, \" sex \ ": \" male \ "}{\" name \": \ "Plum \", \ "age \": 41, \ "sex \": \ "female \" }{\ "name \": \ "Qian \", \ "age \": 15, \ "sex \": \ "female \"
"In json format, the above data starts with (braces), and the following strings start with (braces), and each group
(
).
 
          Gson gson=new Gson();

Type listpyte= TypeToken< LinkedList<User> >(){}.getType();
         LinkedList<User> users=                  (Iterator<User> iterator=              User user=             System.out.println(user.getName()+","+user.getAge()+","+         }
 

3. The Class Object implementing data parsing is the User class here.

                                                        .name =                                   setAge(          .age =                                           .sex =  }

4. Before running, copy the Google Gson-xxx.jar file to the libs folder in the project.

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.