Java parses Json data using Gson and javagson parses json data

Source: Internet
Author: User

Java parses Json data using Gson and javagson parses json data

1. Json data can be parsed using JsonReader In the Android API

2. You can introduce the jar package of Gson and parse Json data with JavaBean.

 

Bean. java

Package com. sphere. json; the data in the public class Bean {/*** Bean must correspond one-to-one to the key value * of the Json data and cannot be changed. * The name corresponds to the name key value of the json data * the wife and json data * otherwise, the read is null */private String name; private String wife; public String getName () {return name;} public void setName (String name) {this. name = name;} public String getWife () {return wife;} public void setWife (String wife) {this. wife = wife ;}}View Code

 

Gson parsing Tool

Package com. sphere. json; import java. io. IOException; import java. io. stringReader; import java. lang. reflect. type; import java. util. arrayList; import java. util. hashMap; import java. util. iterator; import java. util. using list; import com. google. gson. gson; import com. google. gson. reflect. typeToken; import com. google. gson. stream. jsonReader; public class JsonUtils {/*** use JsonReader * @ param data */public void parseJson (String data) {try {// create the JsonReader object JsonReader reader = new JsonReader (new StringReader (data); // start parsing the array reader. beginArray (); while (reader. hasNext () {// starts parsing the object reader. beginObject (); while (reader. hasNext () {// obtain the key String tagName = reader. nextName (); if (tagName. equals ("name") {System. out. print ("name --->" + reader. nextString ();} else if (tagName. equals ("wife") {System. out. println ("wife --->" + reader. nextString () ;}// parse object ends reader. endObject ();} // parse the array to end reader. endArray ();} catch (IOException e) {// TODO Auto-generated catch block e. printStackTrace () ;}// parses the json array and returns the list object public ArrayList <HashMap <String, String> parseBeanFromJson (String jsonArray) {// Gson parses the Json array Type = new TypeToken <shortlist <Bean >> (){}. getType (); Gson gson = new Gson (); consumer list <Bean> beans = gson. fromJson (jsonArray, type); ArrayList <HashMap <String, String> list = new ArrayList <HashMap <String, String> (); for (Iterator iterator = beans. iterator (); iterator. hasNext ();) {Bean bean = (Bean) iterator. next (); // System. out. print (bean. getName (); // System. out. println (bean. getWife (); HashMap <String, String> map = new HashMap <String, String> (); map. put ("name", bean. getName (); map. put ("wife", bean. getWife ();/* Add to list */list. add (map);}/* traverse list */int index = 0; for (Iterator iterator = list. iterator (); iterator. hasNext ();) {HashMap <String, String> hashMap = (HashMap <String, String>) iterator. next (); System. out. println ("the" + (++ index) + "element" + hashMap);} return list ;}}

 

 

Test. java

Package com. sphere. json; import com. google. gson. gson; public class Test {static String jsonArray = "[{\" name \ ": \" Zhang Wuji \ ", \" wife \ ": \" minmintermoore \"}, {\ "name \": \ "Yang Guo \", \ "wife \": \ "Little Dragon girl \"}] "; static String jsonData = "{\" name \ ": \" Zhang Wuji \ ", \" wife \ ": \" minmintermoore \"}"; public static void main (String [] args) {new JsonUtils (). parseJson (jsonArray); System. out. println ();/*********** parse Json data ***********/Gson gson = new Gson (); bean user = gson. fromJson (jsonData, Bean. class); System. out. println ("My name is" + user. getName () + "My wife is" + user. getWife (); System. out. println ();/********** parse the Json array ***********/new JsonUtils (). parseBeanFromJson (jsonArray );}}

 

 

Output result:

Name ---> Zhang Wuji wife ---> minmintermoore name ---> Yang Guo wife ---> my name is Zhang Wuji, and my wife is 1st elements in the minmintermoore list {wife = minmintermoore, name = Zhang Wuji} 2nd elements in the list {wife = maid, name = Yang Guo}

 

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.