Android Json解析服务器文件
给你我的一个代码片段,自己去理解吧
解析代码:
/
解析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}]";
}
看别人使用的类:主要用到的类有:orgapachehttpclientHttpClient 、orgapachehttpclientmethodsHttpPost 和orgjsonJSONObject,应该包含这个方法的
0条评论