乾貨集中營獲取到的json資料格式為
]這種格式的json資料解析時找到兩種方式:
1.先將data後邊的資料解析為json陣列,然後遍歷json陣列,利用gson解析成物件形式
jobject =
newjsonparser()
.parse
(jsondata)
.getasjsonobject()
; jsonarray jsonarray = jobject.
getasjsonarray
("data");
for(jsonelement info:jsonarray
).gettype()
);list.
add(infogson);}
jaso資料對應的類
public
class
infogson
extends
baseobservable
public
void
settitle
(string title)
public
infogson
(string _id, string author, string category, string createdat, string desc, list images, string likecounts, string publishedat, string stars, string type, string url, string views, string title)
private string _id;
private string author;
private string category;
private string createdat;
private string desc;
private list images;
private string likecounts;
private string publishedat;
private string stars;
private string type;
private string url;
private string views;
private string title;
@bindable
public string get_id()
public
void
set_id
(string _id)
@override
public string tostring()
';}@bindable
public string getauthor()
public
void
setauthor
(string author)
@bindable
public string getcategory()
public
void
setcategory
(string category)
@bindable
public string getcreatedat()
public
void
setcreatedat
(string createdat)
@bindable
public string getdesc()
public
void
setdesc
(string desc)
@bindable
public list getimages()
public
void
setimages
(list images)
@bindable
public string getlikecounts()
public
void
setlikecounts
(string likecounts)
@bindable
public string getpublishedat()
public
void
setpublishedat
(string publishedat)
@bindable
public string getstars()
public
void
setstars
(string stars)
@bindable
public string gettype()
public
void
settype
(string type)
@bindable
public string geturl()
public
void
seturl
(string url)
@bindable
public string getviews()
public
void
setviews
(string views)
@bindingadapter()
public
static
void
loadimage
(imageview view,string url)
}
2.利用gson直接從data開始解析,定義乙個內部類
public
class
datas
public
void
set_id
(string _id)
public string getauthor()
public
void
setauthor
(string author)
public string getcategory()
public
void
setcategory
(string category)
public string getcreatedat()
public
void
setcreatedat
(string createdat)
public string getdesc()
public
void
setdesc
(string desc)
public list getimages()
public
void
setimages
(list images)
public string getlikecounts()
public
void
setlikecounts
(string likecounts)
public string getpublishedat()
public
void
setpublishedat
(string publishedat)
public string getstars()
public
void
setstars
(string stars)
public string gettype()
public
void
settype
(string type)
public string geturl()
public
void
seturl
(string url)
public string getviews()
public
void
setviews
(string views)
public string gettitle()
public
void
settitle
(string title)
}public list
getdata()
public
void
setdata
(list
data)
} datas data= gson.
fromjson
(jsondata,
newtypetoken
().gettype()
);log.d(
"retrofit"
,"onresponse: "+(
(datas)data)
.data.
get(1)
.geturl()
);
兩種方法都能成功解析,第二種比較簡單,但是實體類相對複雜一點。 json資料解析
由於是從網路上面讀取json,因此需要讀取網路檔案 url url new url path httpurlconnection conn httpurlconnection url.openconnection 利用httpurlconnection物件,我們可以從網路中獲取網頁資料.conn.s...
json資料解析
今天在做easyui列表頁面回顯字典表資料時候遇到乙個json解析的奇怪問題,首先我用spring gethibernatetemplate 中的createsqlquery進行資料查詢 查出來的資料是 2,2,2,2,2,2,2,之類的 public list findbypage final s...
Json 資料解析
假設現在要建立這樣乙個json文字 物件 married false 布林值 try 新建people物件 jsonarray phone new jsonarray phone是個陣列 phone.put 123231 put 1231234 把 號碼放到phone裡面 people.put ph...