Android--------parsing JSON files using Gson

Source: Internet
Author: User
<span id="Label3"></p><p># #使用gson解析json文件<br><br>There are two forms of **json: * *<br><br>**1. {} type, and data is included with {}; * *<br><br>**2. [] type, that is, data is included with []; * *<br><br><br><br><br>Here is an example of how Gson parses json, using only the ~<br><br>Send two JSON content first<br><br>1. The outermost layer is {}<br><br>{<br>"resp": "ok",<br>"result": {<br>"date": "2013-4-19 16:55:58",<br>"focus": [<br>{<br>"summary": "introduction",<br>"title": "caption",<br>"url": "http://www.ceshi.com"<br>},<br>{<br>"summary": "introduction 22",<br>"title": "heading 22",<br>"url": "http://www.ceshi22.com"<br>}<br>],<br>"items": [<br>{<br>"id": "11",<br>"name": "names",<br>"price": "88.88"<br>},<br>{<br>"id": "22",<br>"name": "22",<br>"price": "100"<br>}<br>]<br>}<br>}<br><br><br>2. The outer layer is []<br><br>[<br>{<br>"addr": "beijing",<br>"age": "22",<br>"name": "zeng"<br>},<br>{<br>"addr": "shanghai",<br>"age": "20",<br>"name": "zhang"<br>},<br>{<br>"addr": "shenzhen",<br>"age": "25",<br>"name": "wang"<br>}<br>]<br><br>###** one, using gson, analysis, The key is to define a good JavaBean class object * *<br><br>1. * * * * definition {} type JavaBean class * * *<br><br>Bean.java<br><br>public class Bean {<br>@SerializedName ("resp")<br>Private String respone;//if The variable is named different from the parsing data, add @serializedname ("")<br>Private String resp;<br>Private result result;<br>//.... Omit set below, get<br><br>Resultbean class<br><br>Result.java<br><br><br>public class Result {<br>Private String date;<br>Private List<focusitem> focus;<br>Private List<item> items;<br>}<br>Focusitem.java<br><br>public class Focusitem {<br>Private String title;<br>Private String summary;<br>Private String url;<br>}<br>Item.java<br><br>public class Item {<br>Private String name;<br>private int id;<br>Private float price;<br>}<br><br>2. * * * * definition {} type JavaBean class * * *<br><br>Info.java<br><br>public class Info {<br>Private String name;<br>private int age;<br>Private String addr;<br>}<br><br><br>###** two, parsing code, One of the outermost is {} one is []**<br><br><br>private void Parse () {<br>Gson Gson = new Gson ();<br>Switch (type) {<br>The outermost layer is {}, encapsulated as an object<br>Case OBJ:<br>Bean Fromjson = Gson.fromjson (readcontext, bean.class);<br>System.out.println (fromjson.tostring ());<br>Break<br>The outermost layer is [], encapsulated as an array object<br>Case ARR:<br>Type type = new typetoken<list<info>> () {}.gettype ();<br>Object fromJson2 = Gson.fromjson (readcontext, type);<br>list<info> list = (list<info>) fromJson2;<br>Break<br>}<br>}</p><p><p>Android--------parsing JSON files using Gson</p></p></span>

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.