Parsing JSON data in Android and actually using it in the ListView

Source: Internet
Author: User

The parsing of JSON data in Android, and the reality in the ListView, the following is the data
{"Ziparea": "410100.0", "EndDate": "2015-04-03 00:00:00", "Ecertarea": "\u9053\u8def\u8d27\u7269\u8fd0\u8f93\u9a7e\ u9a76\u5458 "," ecertstate ":" \u4ece\u4e1a "," begindate ":" 2009-04-03 18:12:30 "," Sex ":" \u7537 "," Birthday ":" 1958-06-25 00:00:00 "," result ": 0," Ecertid ":" 4101010020009000363 "," addr ":" \u90d1\u5dde\u5e02\u4e2d\u539f\u533a\ U4f0f\u725b\u5357\u8def\u79e6\u5cad\u5c0f\u533a4\u53f7\u697c54\u53f7 "," name ":" \u5b54\u7965\u6c11 "}

It is better to define an entity class (A.class) corresponding data first.
If your josn is in the default josn format, that's it.

Josnarray List=josnarray ("Josn data above You");
List<a> l=new arraylist<a> ();
A;
for (Jsonobject o:list)
{
A=new A ();
A.setxxx (O.getstring ("Built in JSON here"))
A.setxxx (O.getstring ("Built in JSON here"))
L.add (a);
}
In this way list<a> L loaded the data directly a.getxxx () OK
First of all, your JSON data should also be added as a key
{"Info": [{"Ziparea": "410100.0", "EndDate": "2015-04-03 00:00:00", "Ecertarea": "\u9053\u8def\u8d27\u7269\u8fd0\ u8f93\u9a7e\u9a76\u5458 "," ecertstate ":" \u4ece\u4e1a "," begindate ":" 2009-04-03 18:12:30 "," Sex ":" \u7537 "," Birthday ":" 1958-06-25 00:00:00 "," result ": 0," Ecertid ":" 4101010020009000363 "," addr ":" \u90d1\u5dde\u5e02\u4e2d\u539f\u533a \u4f0f\u725b\u5357\u8def\u79e6\u5cad\u5c0f\u533a4\u53f7\u697c54\u53f7 "," name ":" \u5b54\u7965\u6c11 "}]}
Then define an entity
Like what

public class Info {private int xx= 0;private String XXX = null;private int xxxx= 0;}


Then parse
Methods are as follows

public static list<info> Getinfos (string key, String jsonstring) {list<info> List = new arraylist<info> (); try {jsonobject jsonobject = new Jsonobject (jsonstring);//returns the JSON array Jsonarray Jsonarray = Jsonobject.getjsonarray ( key); for (int i = 0; i < jsonarray.length (); i++) {Jsonobject jsonObject2 = Jsonarray.getjsonobject (i); info info = new Info. SETXX (jsonobject2.getint ("XX")); info. setxxx (jsonobject2.getstring ("XXX")); info. setxxx ( Jsonobject2.getint ("XXXX")); List.add (info);}} catch (Jsonexception e) {e.printstacktrace ();} return list;}



The key in the above code is the key added to the JSON data.


Parsing JSON data in Android and actually using it in the ListView

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.