First reprinted from http://www.cnblogs.com/androidsuperman/p/4579249.html thanks @ Northwest Wild Wolf classmate.
The first part: Gsonformat plug-in basic use method
Long ago, write JSON parsing with the original method of parsing JSON, and later in order to speed up the development process, began to use Gson,fastjson and other third-party jar package for JSON parsing, in order to keep the apk small enough, not because the introduction of JAR package caused the apk file too large, Choose to use Gson to speed up JSON parsing, of course, last year Androidstudio 1.0 release, many people began to use Androidstudio, of course, studio has a lot of easy to develop plug-ins to accelerate the development of applications, reduce the developer's workforce.
Today recommended a tool is: Gsonformat:https://github.com/zzz40500/gsonformat after use, how so sour, haha installation process:setting-->plugins--> Use: Customize a JavaBean (Nothing, just an empty class) first copy the JSON you want to parse and then alt+insert the following interface to paste into the following interface: Click OK to automatically generate the corresponding JavaBean statement: then JSON to The conversion of beans provides two tool classes:
1 Import java.util.List; 2/** 3 * Todo:json Tool Class 4 * 5 * @author soyoungboy 6 * @date 2014-11-8 PM 2:32:24 7 */8 public ABS Tract class JSON {9 private static JSON json;10 JSON () { }12 public static JSON get () {() ( JSON = = null) {+ json = new Gsonimpl (); }16 return json;17 }18 public abstract String ToJson ( Object src); public abstract <T> T toobject (String json, class<t> Claxx); public Abstract < T> T toobject (byte[] bytes, class<t> claxx), public abstract <T> list<t> toList (String json, Class<t> Claxx); 22}
1 public class Gsonimpl extends Json {2 private Gson Gson = new Gson (), 3 @Override 4 public String ToJson (Ob ject src) {5 return Gson.tojson (SRC); 6 } 7 @Override 8 public <T> T toobject (String json, CLASS< ; T> Claxx) {9 return Gson.fromjson (JSON, Claxx), }11 @Override12 public <T> T Toobject ( byte[] bytes, class<t> claxx) { return Gson.fromjson (New String (bytes), claxx); }15 @ Override16 public <T> list<t> toList (String json, class<t> claxx) { Type type = new TypeTok En<arraylist<t>> () {}.gettype (); list<t> List = Gson.fromjson (JSON, type); return list;20 }21}
Then there is a sentence that perfectly parses the JSON:
- 1 JavaBean Bean =gsonimpl.get (). Toobject (Json,javabean.class);
The perfect parsing of JSON is so simple that it's so sour. The tool class for parsing JSON comes from the wheel code of Lite's GitHub. Https://github.com/litesuits/android-lite-http/tree/master/library/src/com/litesuits/http/data
Part II: Using Gsonformat to parse and wind weather interface data Wind Weather: http://www.heweather.com/ 3,000 visits per day, enough for our amateurs, to make a weather forecast demo is good. First is the weather data obtained from wind weather, Jason format, put on jason.parse.online.fr to see this: but!!!!! According to the first part of the method, a Jason data copied in, click OK you will find it prompt Parse err! Why, because this and the wind weather wonderful Jason data, the first key is "Heweather Data Service 3.0", where the space, "." is illegal, causing it to parse out the corresponding entity class. What about that? First, when parsing the first line of the interface method's Jason data (the left part), the first key, which is "Heweather Data Service 3.0", is replaced by the " Heweatherdataservice ", remove the space and 3.0, copy into the Gsonformat plug-in Paste board, click OK, you will find that the resolution is successful. Then, the JSON raw data returned by the Web page is also processed and parsed. Entity classes and the data to be parsed, of course, to be consistent, right? ~ here I used the Stringbulder Deletecharat method, according to the string subscript to remove the space and "3.0", and then, with the first line of code, JSON parsing done ~ ~ ~ Debugging look at my Weatherbean This object, the data are already obediently in there!! Summary: Made me long time, this format is not standard Jason data, but also found the processing method, interface to get the Jason data if not standard, you can first deal with the format of the specification, with Jasonformat plug-in to parse, The program is then processed in the same format as the code after it gets to Jason string in HTTP mode. This way, the entity class and the Jason string correspond in the same format. Next, it's a line of code that's done ..... is not this JSON parsing can be done in 2 minutes??? ~~~ Likes please! .
Android Gsonformat Plugin parsing jason Data + Wind Weather interface parsing case