Android Json解析服务器文件,第1张

给你我的一个代码片段,自己去理解吧

解析代码:

/

解析json数据

@param context

源数据

/

private void parseJson(String json)

{

try

{

JSONArray json_1 = new JSONArray(json);

List<Course> list = new ArrayList<Course>();

for (int i = 0; i < json_1length(); i++)

{

JSONObject json_2 = json_1getJSONObject(i);

Systemoutprintln("第一层第" + (i + 1) + "条数据="

+ json_2getString("Name") + "ID:"

+ json_2getInt("ID") + "父级ID:"

+ json_2getInt("ParentID"));

parese(json_2);//TODO

JSONArray json_3 = json_2getJSONArray("Children");

if (json_3length() != 0)

{

for (int j2 = 0; j2 < json_3length(); j2++)

{

JSONObject json_4 = json_3getJSONObject(j2);

Systemoutprintln("第二层第" + (i + 1) + "条数据="

+ json_4getString("Name") + "ID:"

+ json_4getInt("ID") + "父级ID:"

+ json_4getInt("ParentID"));

JSONArray json_5 = json_4getJSONArray("Children");

if (json_5length() != 0)

{

for (int k = 0; k < json_5length(); k++)

{

JSONObject json_6 = json_5getJSONObject(k);

Systemoutprintln("第三层第" + k + "条数据"

+ json_6getString("Name") + "ID:"

+ json_6getInt("ID") + "父级ID:"

+ json_6getInt("ParentID"));

}

}

}

}

}

}

catch (JSONException e)

{

eprintStackTrace();

}

}

数据源:

public class Cosnt

{

public static final String SOURCE = "[{Name:语言知识与语言运用,ID:7332,Children:[{Name:字音,ID:7333,Children:[{Name:字音,ID:7334,Children:[],ParentID:7333}],ParentID:7332},{Name:字形,ID:7339,Children:[],ParentID:7332},{Name:词语,ID:7345,Children:[],ParentID:7332},{Name:句子,ID:7375,Children:[],ParentID:7332},{Name:标点符号,ID:7381,Children:[],ParentID:7332},{Name:修辞,ID:7387,Children:[],ParentID:7332},{Name:语言运用与语言表达,ID:7390,Children:[],ParentID:7332}],ParentID:0},"

+ "{Name:文化、文学常识和名篇名句,ID:7405,Children:[{Name:作家作品,ID:7406,Children:[],ParentID:7405},{Name:古代文化常识,ID:7428,Children:[],ParentID:7405},{Name:名篇名句,ID:7431,Children:[],ParentID:7405}],ParentID:0},"

+ "{Name:文言文,ID :7434,Children:[{Name:文言实词,ID:7435,Children:[],ParentID:7434},{Name:文言虚词,ID:7439,Children:[],ParentID:7434},{Name:文言句式,ID:7442,Children:[],ParentID:7434},{Name:文言文阅读,ID:7445,Children:[],ParentID:7434}],ParentID:0}]";

}

那要看你的json数据是什么要的格式,比如javabean实体中包含实体的转换

JSONObject jsonObject = JSONObjectfromObject("你的json数据");

Map<String, Object> classMap = new HashMap<String, Object>();

/如:{"validateMessagesShowId":"_validatorMessage","status":true,"httpstatus":200,"data":{"otherMsg":"","loginCheck":"Y"},"messages":[],"validateMessages":{}}

/

classMapput("validateMessages", 你的Javabean实体class);

classMapput("data", "你的javabean实体名称class");

\\ MessagesShow 实体包含 你的javabean实体

/

public class MessagesShow {

private String validateMessagesShowId;

private boolean status;

private String httpstatus;

private List<String> messages;

private ValidateMessages validateMessages;

private MessagesData data ;

//set 和 get 方法

}

/

MessagesShow messages =(MessagesShow) JSONObjecttoBean(jsonObject, MessagesShowclass, classMap);

json文件是一种轻量级的数据交互格式。一般在jquery中使用getJSON()方法读取。

$getJSON(url,[data],[callback])

url:加载的页面地址

data: 可选项,发送到服务器的数据,格式是key/value

callback:可选项,加载成功后执行的回调函数

1首先建一个JSON格式的文件userinfojson 保存用户信息。如下:

1234567891011121314151617

[{"name":"张国立","sex":"男","email":"zhangguoli@123com"},{"name":"张铁林","sex":"男","email":"zhangtieli@123com"},{"name":"邓婕","sex":"女","email":"zhenjie@123com"}]

2其次建一个页面用于获取JSON文件里的用户信息数据,并显示

123456789101112131415161718192021222324252627282930313233343536373839404142

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 10 Transitional//EN" "http://wwww3org/TR/xhtml1/DTD/xhtml1-transitionaldtd"><html xmlns="http://wwww3org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>getJSON获取数据</title><script type="text/javascript" src="js/jquery-182minjs"></script><style type="text/css">#divframe{ border:1px solid #999; width:500px; margin:0 auto;}loadTitle{ background:#CCC; height:30px;}</style>< script type = "text/javascript" >$(function (){ $("#btn")click(function () { $getJSON("js/userinfojson", function (data){ var $jsontip = $("#jsonTip"); var strHtml = "123"; //存储数据的变量 $jsontipempty(); //清空内容 $each(data, function (infoIndex, info){ strHtml += "姓名:" + info["name"] + "<br>"; strHtml += "性别:" + info["sex"] + "<br>"; strHtml += "邮箱:" + info["email"] + "<br>"; strHtml += "<hr>" }) $jsontiphtml(strHtml); //显示处理后的数据 }) })})</script></head><body><div id="divframe"><div class="loadTitle"><input type="button" value="获取数据" id="btn"/></div><div id="jsonTip"></div></div></body></html>

DABAN RP主题是一个优秀的主题,极致后台体验,无插件,集成会员系统
网站模板库 » Android Json解析服务器文件

0条评论

发表评论

提供最优质的资源集合

立即查看 了解详情