JSON:
{
"data": {
"advertise": [
],
"button": [
{
"createTime": null,
"homePage": "1",
"id": 49,
"idcode": "49",
"name": "jj",
"positionDesc": "jjjj",
"type": "1",
"url": "imagery",
"value": "a1"
},
{
"createTime": null,
"homePage": "1",
"id": 51,
"idcode": "51",
"name": "aaa",
"positionDesc": "dsadsad",
"type": "1",
"url": "Adsadasdasdasd",
"value": "a4"
},
{
"createTime": null,
"homePage": "1",
"id": 52,
"idcode": "52",
"name": "dsadasd",
"positionDesc": "dwadwad",
"type": "1",
"url": "asdfaefwaf",
"value": "a7"
},
{
"createTime": null,
"homePage": "1",
"id": 53,
"idcode": "53",
"name": "faefsdfew",
"positionDesc": "fefadsawef",
"type": "3",
"url": "fdsfjasio",
"value": ""
},
{
"createTime": null,
"homePage": "0",
"id": 54,
"idcode": "54",
"name": "dsfe",
"positionDesc": "gruel",
"type": "3",
"url": "fdsfweffsd",
"value": ""
},
{
"createTime": null,
"homePage": "0",
"id": 56,
"idcode": "56",
"name": "啦啦啦",
"positionDesc": "放到沙发",
"type": "1",
"url": "立即",
"value": ""
},
{
"createTime": null,
"homePage": "0",
"id": 57,
"idcode": "57",
"name": "few 粉",
"positionDesc": "放到沙发",
"type": "1",
"url": "科技哦叫",
"value": ""
}
],
"flag": "1",
"hint": "对萨大误区",
"notification": [
{
"content": " 是豆腐干大师傅个阿斯顿发生阿斯弗",
"createTime": "2015-10-13 11:52:33",
"flag": "1",
"id": "50",
"title": "阿斯顿发射点"
},
{
"content": " 发顺丰阿斯顿发生法",
"createTime": "2015-10-13 11:52:20",
"flag": "1",
"id": "49",
"title": "飞洒发生"
}
],
"point": "10"
},
"msg": "ok",
"status": 1
}
首先在需要用到的地方初始化:
MyJsonHelp help = new MyJsonHelp(json);(json是上面那段json)
把这个json看成三层:最外层三个字断(msg,status,data),然后是data中的第二层的字断(flag, hint,notification等,以及三个jsonarray:notification,advertise,button),最后是三个jsonarray中的内容,看成第三层,
然后开始用这个工具类解析第一层的数据:
String str = help.getValueByKey("msg");
String str = help.getValueByKey("status");然后解析第二层的数据:
首先要重新设置 jsonobject的解析内容:
help.reSetJsonObject("data");然后同样的调用 getValueByKey()方法获取对应的key所对应的value,
然后再开始解析最后一层的数据,因为三个jsonarray是并列的层级关系,同属于data中,在解析下一层的时候,让jsonobject定位到jsonarray中解析其中的内容,所以当再次解析同一层级的jsonarray时需要重新定位到这个jsonarray的层级中,
解析jsonarray的方法是这样的:
第一次循环不调用
help.reSetJsonObject("data");
因为在第一次解析jsonarray时跟它实在同一层级中,在for循环中会解析里面的内容,这是会定位到第三层中,所以解析其他的jsonarray时要重新定位到第二层中,
在reSetJsonObject()方法中已经对 jsonarray进行了初始化
for (int i = 0; i < help.getArrayCountByKey("advertise"); i++) { str = str + help.getArrayValueByKey(i, "") + "\n"; } str = str + "-------------------------\n"; help.reSetJsonObject("data"); for (int i = 0; i < help.getArrayCountByKey("button"); i++) { str = help.getArrayValueByKey(i, "createTime"); str = help.getArrayValueByKey(i, "homePage"); str = help.getArrayValueByKey(i, "id"); str = help.getArrayValueByKey(i, "idcode"); str = help.getArrayValueByKey(i, "name"); str = help.getArrayValueByKey(i, "positionDesc"); str = help.getArrayValueByKey(i, "type"); str = help.getArrayValueByKey(i, "url"); str = help.getArrayValueByKey(i, "value") + "\n"; } str = str + "-------------------------\n"; help.reSetJsonObject("data"); for (int i = 0; i < help.getArrayCountByKey("notification"); i++) { str = help.getArrayValueByKey(i, "content"); str = help.getArrayValueByKey(i, "createTime"); str = help.getArrayValueByKey(i, "flag"); str = help.getArrayValueByKey(i, "id"); str = help.getArrayValueByKey(i, "title") + "\n"; }
最后获取jsonarray中的值时调用 getArrayValueByKey()方法传入对应的 i ,以及 key获取相对应的值。
对于获取值时的空值有做过简单的处理,这个工具类感觉还可以再优化,因为知识简单的节省了一些代码量还做不到那些开源的功能那么强大
本文纯属个人原创,转载请注明出处,谢谢。
源码:
import android.util.Log;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
/**
* json 解析帮助类,
* 用于简化手动解析json时大量代码。
*
* Created by langevorbei on 15/10/15.
*/
public class MyJsonHelp {
private JSONObject jsonObject = null;
private JSONArray jsonArray = null;
/**
* 初始化构造方法
*
* @param result 获取的json
*/
public MyJsonHelp(String result) {
try {
jsonObject = new JSONObject(result);
} catch (JSONException e) {
e.printStackTrace();
}
}
/**
* 重新设置 jsonobject 的解析内容
*
* @param key 需要解析的键
*/
public void reSetJsonObject(String key) {
String str = getValueByKey(key);
try {
jsonObject = new JSONObject(str);
} catch (JSONException e) {
e.printStackTrace();
}
}
/**
* 获取 key 对应的值
*
* @param key 要获取的 key
* @return 返回 key 所对应的值
*/
public String getValueByKey(String key) {
String str = "";
if (null != jsonObject && jsonObject.has(key)) {
try {
str = jsonObject.getString(key);
} catch (JSONException e) {
e.printStackTrace();
}
return str;
}
return str;
}
/**
* 获取 jsonarray 的长度以及初始化 jsonarray
*
* @param key 要获取的 key
* @return 返回 key 所对应的值
*/
public int getArrayCountByKey(String key) {
int count = 0;
try {
if (null != jsonObject && jsonObject.has(key)) {
jsonArray = jsonObject.getJSONArray(key);
count = jsonArray.length();
} else {
return 0;
}
} catch (JSONException e) {
e.printStackTrace();
}
return count;
}
/**
* 获取当前位置的 jsonarray 中 key 对应的值
*
* @param index jsonarray做 for 循环时的位置
* @param key 要获取的 key
* @return 返回 key 所对应的值
*/
public String getArrayValueByKey(int index, String key) {
String str;
try {
if (null != jsonArray.optJSONObject(index) && jsonArray.optJSONObject(index).has(key)) {
str = jsonArray.optJSONObject(index).getString(key);
return str;
}
} catch (JSONException e) {
e.printStackTrace();
}
return "";
}
}