當前位置:成語大全網 - 書法字典 - android如何看待json數據格式

android如何看待json數據格式

向服務器發送請求後,服務器會返回壹系列數據,大多數情況下是XML格式或者JSON格式。那麽JSON解析相對XML來說就比較方便了,所以先說JSON解析。

JSON有幾種基本的數據格式:

1.JSON對象——JSON對象

{ "姓名":"胡小衛","年齡":20,"男":真}

2.JSON數組-JSON數組

[{ "姓名":"胡小衛","年齡":20,"男性":真},{ "姓名":"趙小亮","年齡":22,"男性":假}]

3.稍微復雜壹些的JSONObject

{ "姓名":"胡小衛","年齡" = 20,"男":真,"地址":{ "街道":"嶽麓山以南","城市":"長沙","鄉村":"中國" }}

4.稍微復雜壹點的JSONArray

[

{ "姓名":"胡小衛","年齡" = 20,"男":真,"地址":{ "街道":"嶽麓山以南","城市":"長沙","國家":"中國" }},

{ "姓名":"趙小亮","年齡" = 22,"男":假,"地址":{ "街道":"九州港","城市":"珠海","國家":"中國" }}

]

5.更復雜的是,這是我從愛幫巴士上撿來的數據。

{ " bus ":{ " bus ":[{ " last _ foot _ dist ":" 0 "," time": "37 "," segments ":{ " segment ":[{ " Line _ name ":"歷山專線(中南大學學生公寓-長沙火車站)")湖南師範大學;兩英裏半;嶽麓山北;市四院;塗畫教育(太平街);牛二教育(南洋街);韭菜園;曙光路口;長島路口;長沙火車站"," end_stat ":"長沙火車站"," line _ xys ":" line _ dist ":" 7535 "," start _ stat ":"嶽麓山以南" }}," foot _ dist ":"362 ","。" Time": "41 "," segments ":{ " segment ":[{ " line _ name ":" LV 1路(科教村-長沙火車站)"," foot _ dist": "337 "," stat _ xys "。湖南師範大學;兩英裏半;嶽麓山北;市四院;塗畫教育(太平街);蔡鄂中路;韭菜園;喬莊;曙光路口;長島路口;五壹東村;車站路口;長沙火車站"," end_stat ":"長沙火車站"," line _ xys ":" line _ dist ":" 7822 "," start _ stat ":"嶽麓山以南" }}," foot _ dist ":"337 ","。

接下來是解析部分。

第壹,直接分析

1.{ "姓名":"胡小衛","年齡":20,"男":真}

Java代碼

public void parseJsonObject(String JSON){

嘗試{

JSON object JSON object = new JSON object(JSON);

string name = JSON object . getstring(" name ");

int age = JSON object . getint(" age ");

boolean male = JSON object . get boolean(" male ");

//下壹步做什麽

} catch (JSONException e) {

// TODO自動生成的catch塊

e . printstacktrace();

}

}

2.[{ "姓名":"胡小衛","年齡":20,"男性":真},{ "姓名":"趙小亮","年齡":22,"男性":假}]

Java代碼

public void parseJSONArray(String JSON){

嘗試{

JSON array JSON array = new JSON array(JSON);

for(int I = 0;我& ltJSON array . length();i++) {

JSON object = JSON array . getjson object(I);

string name = JSON object . getstring(" name ");

int age = JSON object . getint(" age ");

boolean male = JSON object . get boolean(" male ");

//下壹步做什麽

}

} catch (JSONException e) {

// TODO自動生成的catch塊

e . printstacktrace();

}

}

3.{ "姓名":"胡小衛","年齡" = 20,"男":真,"地址":{ "街道":"嶽麓山以南","城市":"長沙","鄉村":"中國" }}

Java代碼

public void parseJsonPerson(String JSON){

嘗試{

JSON object JSON object = new JSON object(JSON);

string name = JSON object . getstring(" name ");

int age = JSON object . getint(" age ");

boolean male = JSON object . get boolean(" male ");

JSON object address JSON = JSON object . getjson object(" address ");

string street = address JSON . getstring(" street ");

string city = address JSON . getstring(" city ");

string country = address JSON . getstring(" country ");

Address address =新地址(街道、城市、國家);

Person person =新人(姓名、年齡、男性、地址);

} catch (JSONException e) {

// TODO自動生成的catch塊

e . printstacktrace();

}

}

4.

[

{ "姓名":"胡小衛","年齡" = 20,"男":真,"地址":{ "街道":"嶽麓山以南","城市":"長沙","國家":"中國" }},

{ "姓名":"趙小亮","年齡" = 22,"男":假,"地址":{ "街道":"九州港","城市":"珠海","國家":"中國" }}

]

Java代碼

public void parseJsonArrayPerson(String JSON){

列表& ltPerson & gtpersons = new ArrayList & ltPerson & gt();

嘗試{

JSON array = new JSON array(JSON);

for(int I = 0;我& ltJSON array . length();i++) {

JSON object = JSON array . getjson object(I);

string name = JSON object . getstring(" name ");

int age = JSON object . getint(" age ");

boolean male = JSON object . get boolean(" male ");

JSON object address JSON = JSON object . getjson object(" address ");

string street = address JSON . getstring(" street ");

string city = address JSON . getstring(" city ");

string country = address JSON . getstring(" country ");

Address address =新地址(街道、城市、國家);

Person person =新人(姓名、年齡、男性、地址);

persons . add(person);

Log.v("juno ",person . tostring());

}

} catch (JSONException e) {

// TODO自動生成的catch塊

e . printstacktrace();

}

Log.v("juno ",persons . tostring());

}

5.{ " bus ":{ " bus ":[{ " last _ foot _ dist ":" 0 "," time": "37 "," segments ":{ " segment ":[{ " Line _ name ":"歷山專線(中南大學學生公寓-長沙火車站)湖南師範大學;兩英裏半;嶽麓山北;市四院;塗畫教育(太平街);牛二教育(南洋街);韭菜園;曙光路口;長島路口;長沙火車站"," end_stat ":"長沙火車站"," line _ xys ":"," line _ dist ":"7535 "," start _ stat ":"嶽麓山以南" }}," foot _ dist ":"362 ",""Time": "41 "," segments ":{ " segment ":[{ " line _ name ":" LV 1路(科教村-長沙火車站)"," foot _ dist": "337 "," stat _ xys "。湖南師範大學;兩英裏半;嶽麓山北;市四院;塗畫教育(太平街);蔡鄂中路;韭菜園;喬莊;曙光路口;長島路口;五壹東村;車站路口;長沙火車站"," end_stat ":"長沙火車站"," line _ xys ":" line _ dist ":" 7822 "," start _ stat ":"嶽麓山以南" }}," foot _ dist ":"337 ","。

Java代碼

公共列表& lt致敬& gtparseJSON(字符串str){

Log.d("返回的JSON數據:",str);

列表& lt致敬& gtbusList = new ArrayList & lt致敬& gt();

嘗試{

JSON object JSON object = new JSON object(str);

JSON object buses JSON = JSON object . getjson object(" bus ");

JSON array busJSArray = buses JSON . getjson array(" bus ");

for(int I = 0;我& ltbusjsarray . length();i++){

JSON object busJSObject = busjsarray . getjson object(I);

JSON object segsJSObject = busjsobject . getjson object(" segments ");

JSON array segJSArray = segsjsobject . getjson array(" segment ");

salu tion ST = new salu tion();

for(int j = 0;j & ltsegjsarray . length();j++){

if(j==0){

JSON object segJSObject = segjsarray . getjson object(j);

ST . set line _ name 1(segjsobject . getstring(" line _ name "));

Log.d("線路名稱",segJSObject.getString("線路名稱"));

ST . set stats 1(segjsobject . getstring(" stats "));

ST . setend _ stat 1(segjsobject . getstring(" end _ stat "));

ST . setstart _ stat 1(segjsobject . getstring(" start _ stat "));

}

if(j==1){

JSON object segJSObject = segjsarray . getjson object(j);

ST . set line _ name 2(segjsobject . getstring(" line _ name "));

Log.d("線路名稱",segJSObject.getString("線路名稱"));

ST . setstats 2(segjsobject . getstring(" stats "));

ST . setend _ stat 2(segjsobject . getstring(" end _ stat "));

ST . setstart _ stat 2(segjsobject . getstring(" start _ stat "));

}

}

bus list . add(ST);

}

返回busList

} catch (JSONException e) {

// TODO自動生成的catch塊

e . printstacktrace();

}

返回null

}

這裏我只是得到了壹些我想要的數據。

第二,用JSON類庫Gson解析JSON。

Gson是Google提供的Java類庫,用於Java對象和JSON數據之間的映射。您可以將JSON字符串轉換成Java對象,反之亦然。

首先,從code.google.com/p/google-gson/downloads/list.下載GsonAPI

Java代碼

Google-gson-1 . 7 . 1-release . zip

然後將GSON-1.7.jar復制到libs。

可以用兩種方法解決。

(1)通過獲取JsonReader對象解析JSON數據:

Java代碼

string JSON data = "[{ \ " username \ ":\ " arthinking \ ",\"userId\":001},{\"username\":\"Jason\ ",\ " userId \ ":002 }]";

嘗試{

JSON reader reader = new JSON reader(new string reader(JSON data));

reader . begin array();

while(reader.hasNext()){

reader . begin object();

while(reader.hasNext()){

string tagName = reader . nextname();

if(tagname . equals(" username "){

system . out . println(reader . nextstring());

}

else if(tagname . equals(" userId "){

system . out . println(reader . nextstring());

}

}

reader . end object();

}

reader . endarray();

}

捕捉(異常e){

e . printstacktrace();

}

②通過將JSON數據映射到壹個對象中,使用Gson對象的fromJson()方法獲得壹個對象數組進行運算。

Java代碼

Type listType = new TypeToken & ltLinkedList & lt用戶& gt& gt(){}.getType();

Gson Gson = new Gson();

LinkedList & lt用戶& gtusers = gson.fromJson(jsonData,list type);

for(Iterator Iterator = users . Iterator();iterator . has next();) {

User User =(User)iterator . next();

system . out . println(user . get username());

system . out . println(user . get userid());

}

我壹般用直接解析,加類庫是最煩的...

還有,抱怨壹下,iteye的編輯弱爆了!!!

重印