android-json-< 13 >

Source: Internet
Author: User

1.Json

2.Json Example

Parse the JSON (a string) directly

  Public classJsonutils { Public voidJsonreader (String str) {//str-shaped as: String jsonstr= "[{\" name\ ": \" hq\ ", \" age\ ": 20},{\" name\ ": \" cyx\ ", \" age\ ": 30}]"; //here parsing reads the JSON array. //Create a JSON parsing objectJsonreader jr=NewJsonreader (NewStringReader (str)); Try {        //the read process is 1. Start reading the array. 2. Start reading objects.        3. Start reading key-value pairs. //(repeat 3) 4. Ends the Read object. 5. End reading of an arrayJr.beginarray ();  while(Jr.hasnext ()) {jr.beginobject ();  while(Jr.hasnext ()) {String name=Jr.nextname (); if(Name.equals ("name")) System.out.println ("Name---" +jr.nextstring ()); Else if(Name.equals ("Age")) System.out.println ("Age---" +jr.nextint ());        } jr.endobject ();    } jr.endarray (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }        }         Public voidParsejsonfromuser () {//1. Create a Gson objectGson Gson =NewGson (); //User user = Gson.fromjson (jsondata, user.class); System.out.println ("Name--->" +user.getname ()); System.out.println ("Age---->" +user.getage ()); }}
View Code

Resolves a single JSON object (string: Only one object) to a similarly structured class object

 Public classJsonutils {//parse a single JSON object into a similarly structured user class     Public voidParseuserfromjson (String jsondata) {//1. Create a Gson objectGson Gson =NewGson (); //2. Call the object's Fromjson method, parse to the user object toUser user = Gson.fromjson (jsondata, user.class); System.out.println ("Name--->" +user.getname ()); System.out.println ("Age---->" +user.getage ()); }}

Resolves multiple JSON objects (strings: multiple JSON objects) into a similarly structured class object

 Public classJsonutils {//Parse JSON Array (multiple JSON objects) into a user list     Public voidParseuserfromjson (String jsondata) {//1. Create a type object to hold multiple user objectsType ListType =NewTypetoken<linkedlist<user>>() {}.gettype (); //2. Create a Gson objectGson Gson =NewGson (); //3. Call the Fromjson method and return to the user (list) after parsinglinkedlist<user> users =Gson.fromjson (Jsondata, ListType);  for(Iterator Iterator =users.iterator (); Iterator.hasnext ();) {User User=(User) iterator.next (); System.out.println ("Name--->" +user.getname ()); System.out.println ("Age---->" +user.getage ()); }            }}

android-json-< 13 >

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.