对象结构以”{”大括号开始,以”}”大括号结束。中间部分由0或多个以”,”分隔的”key(关键字)/value(值)”对构成,关键字和值之间以”:”分隔,语法结构如下
{
key1:value1,
key2:value2,
}其中关键字是字符串,而值可以是字符串,数值,true,false,null,对象或数组
数组结构以”[”开始,”]”结束。中间由0或多个以”,”分隔的值列表组成,语法结构如下
[
{
key1:value1,
key2:value2
},
{
key3:value3,
key4:value4
}
]
给服务端发送请求后,服务端会返回一连串的数据,这些数据在大部分情况下都是XML格式或者JSON格式。然后JSON相对XML来说解析相对方便一些,所以先说说JSON的解析。
JSON的基本数据格式有这几种:
1一个JSON对象——JSONObject
{"name":"胡小威" , "age":20 , "male":true}
2一个JSON数组——JSONArray
[{"name":"胡小威" , "age":20 , "male":true},{"name":"赵小亮" , "age":22 , "male":false}]
3复杂一点的JSONObject
{"name":"胡小威", "age"=20, "male":true, "address":{"street":"岳麓山南", "city":"长沙","country":"中国"}}
4复杂一点的JSONArray
[
{"name":"胡小威", "age"=20, "male":true, "address":{"street":"岳麓山南", "city":"长沙","country":"中国"}},
{"name":"赵小亮", "age"=22, "male":false, "address":{"street":"九州港", "city":"珠海","country":"中国"}}
]
android如果是通过>JSONObject json = new JSONObject(); jsonput(key,value); String s = jsontoString(); >给你我的一个代码片段,自己去理解吧解析代码:
/
解析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}]";
}java用服务端怎么用json数据传输到安卓客户端
public class MainActivity extends Activity
{
private Button submit = null;
URL url = null;
String urlPath = ">
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)