Professional Documents
Culture Documents
Document 61 Json Nguyen Thuy
Document 61 Json Nguyen Thuy
Bi
60
Tng tc dch v
Web dng JSON
nguyn thy
Lp trnh Android
Ni dung bi hc
JSON l g?
Cu trc lp, i tng, thnh phn
ca Json Object, Json Array
Cch c d liu dng Json
Lp trnh Android
Lp trnh Android
V tu thuc vo d liu cn trao i, JSON c th c nhiu dng khc nhau, tuy nhin c
th tng hp nhng hai dng chnh sau:
Mt i tng Object cha cc cp gi tr string/value khng cn th t, c bao
trong cp {}, cc gi tr bn trong c nh dng string:value v chia cch nhau bi
du ,. Value y c th l chui, s, true- false, nullC th xem m t cng v d
sau:
V d:
Lp trnh Android
Lp trnh Android
Lp trnh Android
I:\Topica\codingthamkhao\SourceCode_CU\BaitapModule4s2356\
HocJsonBai1
Lp trnh Android
Nn to 1
JSonFactory
Lp trnh Android
Nn to 1
JSonFactory
Lp trnh Android
c d liu t
JSOnArray
protected
ArrayList<Customer>
doInBackground(String... params) {
ArrayList<Customer> dsCustomer=new ArrayList<>();
try
{
JSONArray jsonArray=
JSonFactory.readJSonArray(params[0]);
if(jsonArray!=null)//nu c d liu
{
//duyt tt c JsonObject bn trong
//JsonArray nh sau:
for(int i=0;i<jsonArray.length();i++)
{
JSONObject jsonObject=
jsonArray.getJSONObject(i);
Customer customer=new Customer();
if(jsonObject==null)continue;//nhy qua k tip
if(jsonObject.has("Name"))//c thuc tnh Name
{
customer.setName(jsonObject.getString("Name"));
}
if(jsonObject.has("City"))
{
customer.setCity(jsonObject.getString("City"));
}
if(jsonObject.has("Country"))
{
customer.setCountry(jsonObject.getString("Country"));
}
dsCustomer.add(customer);
Ths. Trn Duy Thanh duythanhcse@gmail.com
0987773061 http://duythanhcse.wordpress.com
Lp trnh Android
END
Ths. Trn Duy Thanh duythanhcse@gmail.com 0987773061 http://duythanhcse.wordpress.com