Json的解析簡記

2021-07-04 19:54:21 字數 3144 閱讀 5643

android在網路通訊的過程中,經常會遇到資料傳輸的問題,而較為常見的資料傳輸的方式則是通過json進行傳輸的。以下是請求伺服器、返回json以及對幾種json的解析的簡單記錄。

1、請求伺服器,返回乙個json陣列:

基本思路就是,先封裝乙個請求伺服器的基類,類的傳入引數是string型別,也就是**,返回值也為string型別,為想得到的json陣列。採用httpclient的方式。具體**如下:

public

class

jsonutil

} catch (exception e)

return stringjson;

}// 初始化httpclient,並設定超時

public

static httpclient gethttpclient()

封裝好之後,在需要獲取json的介面,呼叫這個方法就能實現獲取json的功能,具體**如下:

public

class

jsonthread

implements

runnable

}

jsonobject jsonobject = new jsonobject(stringjson);

int num = jsonobject.getint("num");

int code= jsonobject.getint("code");

string name = jsonobject.getstring("name");

2、,]};

這種json格式的解析,先遍歷出每個欄位再放到陣列裡面:

jsonobject jsonobject = new jsonobject(stringjson);

jsonarray = jsonobject.getjsonarray("list");

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

break

case

1: string str1 = ;

break

}}

這樣就可以得到list裡面兩個子陣列,如果只想得到陣列裡面的字段,則直接遍歷即可。

3、 }

private

void

isnettest() else

}handler handler = new handler() else

break;

default:

break;}}

};private

class

userthread

implements

runnable catch (jsonexception e)

msg.what = 0;

handler.sendmessage(msg);

}}

4、,」arr_affter」:,」arr_stay」:}}

customer_bestreturn = jsonutil.loginserver(customer_best);

jsonobject2 = new jsonobject(customer_bestreturn);

item2 = jsonobject2.getjsonobject("results");

item2_1 = item2.getjsonobject("arr_come");

arr_come_num = item2_1.getint("num");

arr_come_time = item2_1.getstring("time");

item2_2 = item2.getjsonobject("arr_stay");

arr_stay_stay = item2_2.getint("stay");

arr_stay_time = item2_2.getstring("time");

item2_3 = item2.getjsonobject("arr_affter");

arr_affter_num = item2_3.getint("num");

arr_affter_time = item2_3.getstring("time");

5、results: [

,

mymenoreturn = jsonutil.loginserver(mymeno);

jsonobject3 = new jsonobject(mymenoreturn);

status = jsonobject3.getstring("status");

jsonarray = jsonobject3.getjsonarray("results");

if ("true"

.equals(status)) else

hashmapmap = new hashmap<>();

map.put("item_image_01", starid);

map.put("item_tv_01", info);

map.put("item_tv_02", time);

map.put("menoid",menoid);

listimageitem.add(map);

}******adapter saimageitems = new ******adapter(home.this,

listimageitem,// 資料來源

r.layout

.activity_home_listview_item,// 顯示布局

new string,

new int);

listview.setadapter(saimageitems);

listview.setonitemclicklistener(new adapterview.onitemclicklistener()

});}

注:陣列在日誌中的輸出,不能直接輸出陣列名稱,而是應該用:
system.out.println(「uyk」 + arrays.aslist(str));

「`

JSON解析 XML解析

json解析 1大括號代表字典 2中括號 小括號 代表陣列 3冒號的左右兩邊分別為鍵和值 key value 4資料和資料之間 用逗號隔開 我們在本機上建立乙個file.xml,進行解析,並取出年齡35 json解析 本地讀取student.xm l檔案nsstring jstr nsstring ...

JSON解析 XML解析

主要是顯示地理位置和天氣以及溫度。對網路請求下來的json資料進行解析。import viewcontroller.h define kweatherurl inte ce viewcontroller property weak,nonatomic iboutlet uilabel citylab...

JSON資料的解析

一 什麼是json json是一種輕量級的資料格式,一般用於資料互動 json的格式很像oc中的字典和陣列 標準json格式的注意點 key必須用雙引號 要想從json中挖掘出具體資料,得對json進行解析。即把json 轉換為 oc資料型別 二 json oc 轉換對照表 對照關係 三 json解...