Android uses its own jsonobject to parse JSON data __json

Source: Internet
Author: User

Import Org.json.JSONArray;
Import org.json.JSONException;
Import Org.json.JSONObject;

Import Android.content.Context;


public void Allinfofromjson (String jsonstr) {/*{" listmsg": "Data Acquisition success",
"List":[
{"Busid": 1, "busline": "Sympathetic", "Busname": "501", "Cityid": 2, "Districtid": 1,
"Firsttime": "8:00", "Lasttime": "19:00", "Price": 4, "version": 0},
{"Busid": 2, "busline": "502", "Busname": "502", "Cityid": 2, "Districtid": 2,
"Firsttime": "2", "Lasttime": "2", "Price": 2, "version": 0},
{"Busid": 3, "busline": "AAA", "Busname": "601", "Cityid": 2, "Districtid": 1,
"Firsttime": "AA", "Lasttime": "AA", "Price": 1, "version": 0}
]
,
"Nversion": 3}*/
try {
Jsonobject jsonobject=new Jsonobject (jsonstr). Getjsonobject ("list");

jsonarray jsonarray=new jsonobject (jsonstr). Getjsonarray ("list");

for (int i=0;i<jsonarray.length (); i++) {

jsonobject jsonobject= (jsonobject) jsonarray.get (i);

String busline=jsonobject.getstring ("busline");
String busname=jsonobject.getstring ("Busname");
Integer cityid=jsonobject.getint ("Cityid");
Integer districtid=jsonobject.getint ("Districtid");
String firsttime=jsonobject.getstring ("Firsttime");
String lasttime=jsonobject.getstring ("Lasttime");
Double price=jsonobject.getdouble ("price");


Bus Bus=new bus (busname,busline,firsttime,lasttime,price,cityid,districtid);
Busservice busservice=new Busservice (context);
Busservice.save (bus);
}

catch (Jsonexception e) {
E.printstacktrace ();
}
}

Related Article

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.