先把项目文件放上来(gif文件另存为zip即可)
http://hi.csdn.net/attachment/201107/25/0_1311594988iFs9.gif
json表达式定义
Json 的基本文法是键值对,Key:Value 通过冒号分隔
key类型就是字符串 “key1“, ”key2“。。。
关键是值类型,json定义了7种值类型:
1) 字符串,2) 数值,3) true, 4) false, 5) null, 甚至 6) 数组和 7) 对象 都可以是值类型。
数组也是一种值,数组同时可以用来包含一系列的值
对象类型也是一种值,对象可以用来包含一系列的键值对
JSON 文件例子, sample.json
{
"key_a":"val_string",
"key_b":100.0,
"key_c":20,
"key_d":true,
"key_f":false,
"key_g":null,
"key_h":
{
"key_h1":"h1",
"key_h2":100.0,
"key_h3":20,
"key_h4":true,
"key_h5":false,
"key_h6":null,
"key_arrary":[1, 2, 3, 4, 5]
},
"key_i":
[
"string",
100.0,
20,
true,
false,
null,
{"key_1":1, "key_2":2, "key_3":3}
]
}
解析JSONObject, depth参数用于打印缩进格式用。
private void parseJsonObject(JSONObject jo, int depth)
{
JSONArray ja = jo.names();
for (int i = 0; i < ja.length(); i ++)
{
String strKey = ja.optString(i);
String strRawJson = jo.opt(strKey).toString();
char cTestChar = (strRawJson.charAt(0));
switch (cTestChar)
{
case '{':
for (int xxx = 0; xxx < depth; xxx++) System.out.print("\t");
System.out.println(strKey + " value is " + "Object");
parseJsonObject(jo.optJSONObject(strKey), depth+1);
break;
case '[':
for (int xxx = 0; xxx < depth; xxx++) System.out.print("\t");
System.out.println(strKey + " value is " + "Array");
parseJsonArray(jo.optJSONArray(strKey), depth+1);
break;
default:
for (int xxx = 0; xxx < depth; xxx++) System.out.print("\t");
System.out.println(strKey + " value is " + strRawJson);
break;
}
}
}
解析JSONArray, depth参数用于打印缩进格式用。
private void parseJsonArray(JSONArray ja, int depth)
{
for (int i = 0; i < ja.length(); i ++)
{
String strRawJson = ja.opt(i).toString();
char cTestChar = (strRawJson.charAt(0));
switch (cTestChar)
{
case '{':
for (int xxx = 0; xxx < depth; xxx++) System.out.print("\t");
System.out.println("[" + i + "]" + " value is " + "Object");
parseJsonObject(ja.optJSONObject(i), depth+1);
break;
case '[':
for (int xxx = 0; xxx < depth; xxx++) System.out.print("\t");
System.out.println("[" + i + "]" + " value is " + "Array");
parseJsonArray(ja.optJSONArray(i), depth+1);
break;
default:
for (int xxx = 0; xxx < depth; xxx++) System.out.print("\t");
System.out.println("[" + i + "]" + " value is " + strRawJson);
break;
}
}
}
测试函数:
private void parseJsonFile()
{
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(".\\raw\\sample.json"));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (null == br)
return ;
String line = null;
String strJson = new String();
try {
while (null != (line = br.readLine()))
strJson += line;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JSONObject root = null;
try {
root = new JSONObject(strJson);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (root == null)
return ;
parseJsonObject(root, 0);
}
执行结果:
key_i value is Array
[0] value is string
[1] value is 100.0
[2] value is 20
[3] value is true
[4] value is false
[5] value is null
[6] value is Object
key_3 value is 3
key_2 value is 2
key_1 value is 1
key_h value is Object
key_arrary value is Array
[0] value is 1
[1] value is 2
[2] value is 3
[3] value is 4
[4] value is 5
key_h1 value is h1
key_h2 value is 100.0
key_h3 value is 20
key_h4 value is true
key_h5 value is false
key_h6 value is null
key_c value is 20
key_b value is 100.0
key_a value is val_string
key_g value is null
key_f value is false
key_d value is true