android解析json时动态获取key

最近项目中有解析json的时候,本来应该很快解决的,但是一看发过来的json数据,

我的乖乖,json数据好多啊有点复杂,关键是这个key是不一定,网上

找了一下,确实有这样的需求和我的一样,于是应用到自己的项目中

贴一下我的json数据:

{"column": 6, 
	"map": 
	    {"5-4":{"check_name": "-", "ip": "192.168.103.174", "num": 23, "group": 0}, 
		"10-5":{"check_name": "-", "ip": "192.168.103.185", "num": 34, "group": 0}, 
		"7-6":{"check_name": "-", "ip": "192.168.103.189", "num": 37, "group": 0}, 
		"7-5":{"check_name": "-", "ip": "192.168.103.182", "num": 31, "group": 0}, 
		"9-2":{"check_name": "-", "ip": "192.168.103.158", "num": 15, "group": 0}, 
		"10-2":{"check_name": "-", "ip": "192.168.103.159", "num": 16, "group": 0}, 
		"2-2": {"check_name": "-", "ip": "192.168.103.195", "num": 42, "group": 0}, 
		"2-3": {"check_name": "-", "ip": "192.168.103.200", "num": 46, "group": 0}, 
		"2-1": {"check_name": "-", "ip": "192.168.103.144", "num": 2, "group": 0}, 
		"2-4": {"check_name": "-", "ip": "192.168.103.143", "num": 50, "group": 0}, 
		"2-5": {"check_name": "", "ip": "", "num": 53, "group": 0}, 
		"1-1": {"check_name": "", "ip": "", "num": 1, "group": 0}, 
		"9-5": {"check_name": "-", "ip": "192.168.103.184", "num": 33, "group": 0}, 
		"1-3": {"check_name": "-", "ip": "192.168.103.199", "num": 45, "group": 0}, 
		"1-2": {"check_name": "-", "ip": "192.168.103.194", "num": 41, "group": 0}, 
		"9-3": {"check_name": "\u963f\u65af\u987f", "ip": "192.168.103.171", "num": 21, "group": 0}, 
		"1-6": {"check_name": "", "ip": "", "num": 60, "group": 0}, 
		"9-1": {"check_name": "-", "ip": "192.168.103.152", "num": 9, "group": 0}, 
		"5-6": {"check_name": "-", "ip": "192.168.103.187", "num": 35, "group": 0}, 
		"10-1": {"check_name": "-", "ip": "192.168.103.153", "num": 10, "group": 0}, 
		"6-6": {"check_name": "-", "ip": "192.168.103.188", "num": 36, "group": 0}, 
		"5-1": {"check_name": "-", "ip": "192.168.103.148", "num": 5, "group": 0}, 
		"6-4": {"check_name": "-", "ip": "192.168.103.175", "num": 24, "group": 0}, 
		"6-5": {"check_name": "-", "ip": "192.168.103.181", "num": 30, "group": 0}, 
		"6-2": {"check_name": "-", "ip": "192.168.103.155", "num": 12, "group": 0}, 
		"6-3": {"check_name": "-", "ip": "192.168.103.162", "num": 18, "group": 0}, 
		"6-1": {"check_name": "-", "ip": "192.168.103.149", "num": 6, "group": 0}, 
		"5-5": {"check_name": "-", "ip": "192.168.103.180", "num": 29, "group": 0}, 
		"4-1": {"check_name": "-", "ip": "192.168.103.147", "num": 4, "group": 0}, 
		"4-2": {"check_name": "-", "ip": "192.168.103.198", "num": 44, "group": 0}, 
		"4-3": {"check_name": "", "ip": "", "num": 48, "group": 0}, 
		"4-4": {"check_name": "", "ip": "", "num": 51, "group": 0}, 
		"9-4": {"check_name": "-", "ip": "192.168.103.178", "num": 27, "group": 0}, 
		"5-3": {"check_name": "", "ip": "", "num": 17, "group": 0}, 
		"5-2": {"check_name": "-", "ip": "192.168.103.154", "num": 11, "group": 0}, 
		"7-3": {"check_name": "-", "ip": "192.168.103.163", "num": 19, "group": 0}, 
		"3-6": {"check_name": "", "ip": "", "num": 58, "group": 0}, 
		"7-1": {"check_name": "-", "ip": "192.168.103.150", "num": 7, "group": 0}, 
		"9-6": {"check_name": "-", "ip": "192.168.103.192", "num": 39, "group": 0}, 
		"3-2": {"check_name": "-", "ip": "192.168.103.197", "num": 43, "group": 0}, 
		"3-1": {"check_name": "-", "ip": "192.168.103.146", "num": 3, "group": 0}, 
		"7-4": {"check_name": "-", "ip": "192.168.103.176", "num": 25, "group": 0}, 
		"10-6": {"check_name": "-", "ip": "192.168.103.193", "num": 40, "group": 0}, 
		"7-2": {"check_name": "-", "ip": "192.168.103.156", "num": 13, "group": 0}, 
		"8-4": {"check_name": "-", "ip": "192.168.103.177", "num": 26, "group": 0}, 
		"8-5": {"check_name": "-", "ip": "192.168.103.183", "num": 32, "group": 0}, 
		"8-6": {"check_name": "-", "ip": "192.168.103.191", "num": 38, "group": 0}, 
		"10-3": {"check_name": "-", "ip": "192.168.103.172", "num": 22, "group": 0}, 
		"10-4": {"check_name": "-", "ip": "192.168.103.179", "num": 28, "group": 0}, 
		"8-1": {"check_name": "-", "ip": "192.168.103.151", "num": 8, "group": 0}, 
		"8-2": {"check_name": "-", "ip": "192.168.103.157", "num": 14, "group": 0}, 
		"8-3": {"check_name": "-", "ip": "192.168.103.170", "num": 20, "group": 0}}, "row": 10}



下面贴代码:

                               JSONObject jsonObject = new JSONObject(jString);
int column = jsonObject.getInt("column");
int row = jsonObject.getInt("row");
JSONObject mapJSON = jsonObject.getJSONObject("map");

                                // 动态获取key值
Iterator iterator = mapJSON.keys();
while (iterator.hasNext()) {
key = iterator.next();
JSONObject keyJSON = mapJSON.getJSONObject(key);
String stu_name = keyJSON.getString("check_name");
String stu_ip = keyJSON.getString("ip");
int num = keyJSON.getInt("num");
int group = keyJSON.getInt("group");
}

嗯,就是这样子

你可能感兴趣的:(android)