单数据{'singer':{'id':01,'name':'tom','gender':'男'}}
多个数据{"singers":[
{'id':02,'name':'tom','gender':'男'},
{'id':03,'name':'jerry,'gender':'男'},
{'id':04,'name':'jim,'gender':'男'},
{'id':05,'name':'lily,'gender':'女'}]} // 普通json数据解析
private void parsejson(string strresult) {
try {
jsonobject jsonobj = new jsonobject(strresult).getjsonobject("singer");
int id = jsonobj.getint("id");
string name = jsonobj.getstring("name");
string gender = jsonobj.getstring("gender");
tvjson.settext("id号"id ", 姓名:" name ",性别:" gender);
} catch (jsonexception e) {
system.out.println("json parse error");
e.printstacktrace();
}
}
//解析多个数据的json
private void parsejsonmulti(string strresult) {
try {
jsonarray jsonobjs = new jsonobject(strresult).getjsonarray("singers");
string s = "";
for(int i = 0; i < jsonobjs.length() ; i){
jsonobject jsonobj = ((jsonobject)jsonobjs.opt(i)).getjsonobject("singer");
int id = jsonobj.getint("id");
string name = jsonobj.getstring("name");
string gender = jsonobj.getstring("gender");
s = "id号"id ", 姓名:" name ",性别:" gender "\n" ;
}
tvjson.settext(s);
} catch (jsonexception e) {
system.out.println("jsons parse error !");
e.printstacktrace();
}
}