At present, the mobile phone and the server data exchange format is generally JSON, and Google provides gson to parse JSON. Download gson:https://code.google.com/p/google-gson/
The download is placed in lib and imported, if an error occurs: Java.lang.NoClassDefFoundError:com.google.gson.Gson
Because the Android-support-v4.jar is not imported, it can be imported.
One, single object generation JSON
Generate the following class, how to generate it?
{ "createdate": "2015-02-01 10:39:50", "id": "1", "name": "Legendary Beauty", "password": "123456"}
First define an account class with the attributes ID, name, password, createdate.
public class Account {private string Id;private string Password;private string name;private string Createdate;public Accou NT () {super ();} Public account (string ID, string password, string name, String createdate) {super (); this.id = Id;this.password = password; THIS.name = Name;this.createdate = CreateDate;} Public String GetId () {return ID;} public void SetId (String id) {this.id = ID;} Public String GetPassword () {return password;} public void SetPassword (String password) {this.password = password;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} Public String Getcreatedate () {return createdate;} public void Setcreatedate (String createdate) {this.createdate = CreateDate;} @Overridepublic String toString () {return "account [id=" + ID + ", password=" + password + ", name=" + name + ", Createdat E= "+ createdate +"]\n\n ";}}
By defining this class, you can use Gson to generate a JSON string.
Generate Account Object SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd hh:mm:ss"); Account Account = new Account ("1", "123456", "Legendary Beauty", Sdf.format (New Date ()));//Use Gson object to generate JSON string Gson Gson = new Gson (); String jsonstring = Gson.tojson (account); LOG.I ("", jsonstring);
Enter the following log
second, parse the JSON string as a single object
Jsonstring has been generated above, and how to parse it into a single object is simple.
Use Gson to parse the JSON string into a single object account Account1 = Gson.fromjson (jsonstring, Account.class); LOG.I ("", account1.tostring ());
Look at the output log.
Iii. generating a JSON array of individual objects
What's the JSON array, similar to the following
[ { "id": "2", "CreateDate": "2015-02-01 11:21:27", "password": "123456", "name": "Legend" }, { "id": "2", "CreateDate": "2015-02-01 11:21:27", "password": "123456", "name": "Beauty" }]
Generate the JSON array code as follows
Account Account2 = new Account ("2", "123456", "Legend", Sdf.format (New Date ())); Account Account3 = new Account ("2", "123456", "Beauty", Sdf.format (New Date ())); list<account> accountlist = new arraylist<account> (); Accountlist.add (Account2); Accountlist.add ( ACCOUNT3); Jsonarray Accountarray = new Jsonarray (); for (int i = 0; i < accountlist.size (); i++) {String accountstr = Gson.tojson ( Accountlist.get (i)); Jsonobject accountobject;try {accountobject = new Jsonobject (ACCOUNTSTR); Accountarray.put (i, accountObject);} catch ( Jsonexception e) {e.printstacktrace ();}} LOG.I ("", accountarray.tostring ());
The output of log is
Four, a JSON array of multiple individual objects resolves to a single object
A JSON array consisting of multiple individual objects is parsed as follows
Parse JSON array list<account> accountList2 = new arraylist<account> (); for (int i=0;i<accountarray.length (); i++) {Jsonobject Jsonobject = null;try {jsonobject = Accountarray.getjsonobject (i);} catch (Jsonexception e) { E.printstacktrace ();} if (jsonobject! = null) {Account Tempaccount = Gson.fromjson (jsonobject.tostring (), account.class); Accountlist2.add ( Tempaccount);}} LOG.I ("AccountList2", accountlist2.tostring ());
Log of output
Or use a faster conversion method.
account[] Accountarrays = new Gson (). Fromjson (Accountarray.tostring (), account[].class); for (int i=0;i< accountarrays.length;i++) {log.i ("Accountarrays", accountarrays[i].tostring ());} Converted to listlist<account> fooslist = Arrays.aslist (accountarrays);
Resolve to list more quickly
More quickly parse into listtype ListType = new typetoken<arraylist<account>> () {}.gettype (); arraylist<account> accslist = new Gson (). Fromjson (Accountarray.tostring (), listtype); LOG.I ("Accslist", accslist.tostring ());
V. Generating JSON for an object nested object
Nested JSON is similar to the following
{" member": { "id": "4", "name": "I Am Legend" }, "id": "4", "CreateDate": "2015-02-02 12:03:32", " password": "888888", " name": "Legendary Beauty"}
There are 2 ways to generate this JSON.
1. Add a member class, add it as you would add the account class.
public class Member {private string Id;private string Name;public Member () {super ();} Public Member (string ID, string name) {super (); this.id = Id;this.name = name;} Public String GetId () {return ID;} public void SetId (String id) {this.id = ID;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} @Overridepublic String toString () {return "Member [id=" + ID + ", name=" + name + "]\n\n";}}
The generated code is as follows
Generate object Nested object Jsonaccount account4 = new Account ("4", "888888", "Legendary Beauty", Sdf.format (New Date ())); Member Member = new Member ("4", "I Am Legend"); String accountstr = Gson.tojson (ACCOUNT4); String memberstr = Gson.tojson (member); Jsonobject object = null;try {jsonobject memberobject = new Jsonobject (memberstr); object = new Jsonobject (accountstr); obj Ect.put ("member", Memberobject);} catch (Jsonexception e) {e.printstacktrace ();} LOG.I ("", object.tostring ());
Log of output
Vi. parsing the JSON of object nesting objects
Account ACCOUNT5 = Gson.fromjson (Object.ToString (), account.class); LOG.I ("JSON for parsing object nesting object", account5.tostring ());//here with IsNull to determine if there is this object, the existence of member Jsonobjectjsonobject Memberobject = Null;if (!object.isnull ("member")) {try {memberobject = Object.getjsonobject ("member");} catch ( Jsonexception e) {e.printstacktrace ();}} Member MEMBER5 = null;if (null! = memberobject) {Member5 = Gson.fromjson (memberobject.tostring (), member.class); LOG.I ("JSON for parsing object nesting object", member5.tostring ());}
Results of the output
7. Another JSON for parsing object nesting objects
Define a class
public class Accountobject {private String id;private string password;private string Name;private string Createdate;priva Te memberobject memberobject = new Memberobject ();p Ublic class Memberobject {private string id;private string name; @Overri Depublic String toString () {return "Memberobject [id=" + ID + ", name=" + name+ "]\n\n";}} @Overridepublic String toString () {return "Accountobject [id=" + ID + ", password=" + password + ", name=" + name + ", CRE Atedate= "+ CreateDate +", "+ memberobject.tostring () +"]\n\n ";}}
Generate JSON and parse
try {jsonobject mobject = new Jsonobject (memberstr); object = new Jsonobject (ACCOUNTSTR); Object.put ("Memberobject", Mobject);} catch (Jsonexception e) {e.printstacktrace ();} Accountobject accountobject = Gson.fromjson (Object.ToString (), accountobject.class); LOG.I ("Accountobject", accountobject.tostring ());
Print out the log
This blog original address: http://www.cnblogs.com/liqw/p/4266209.html
Demo Download Link: http://files.cnblogs.com/files/liqw/LauncherActivity.zip
Android uses Gson to generate or parse JSON