Android 解析 Json 数据

本文详细介绍了解析不同格式JSON数据的方法,包括简单的键值对、包含数组的JSON对象以及多层级的复杂JSON数据,并提供了Java代码示例。

一,首先要了解一下什么事json数据格式。

这个是百度百科的地址,里面讲解json很详细。(http://baike.baidu.com/link?url=I6SDPpq16bR9QHAhPwDQgPFtTLTIQItaCpYwAJBwHk6pVYPyCEA2s5jrxEyrQhlr


二,解析json数据。

1,解析这个格式的json数据(最简单json数据)

{ "firstName": "Brett", "lastName":"McLaughlin", "email": "aaaa" }
解析代码如下:
	public static String parseSimpleJson(String json){
		String str = "";
		try {
			JSONObject jsonObject = new JSONObject(json);
			str = jsonObject.getString("firstName");
			str = str + jsonObject.getString("lastName");
			str = str + jsonObject.getString("email");
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return str;
	}

2,解析这个格式json数据(比较简单的json数据)

{  "people": { "firstName": "Brett", "lastName":"McLaughlin", "email": "aaaa" }}
解析代码如下:
public static String parseJson(String json){
		String str = "";
		try {
			JSONObject jsonObject = new JSONObject(json);
			JSONObject jsonObject2 = jsonObject.getJSONObject("people");
			str = jsonObject2.getString("firstName");
			str = str + jsonObject2.getString("lastName");
			str = str + jsonObject2.getString("email");
		} catch (JSONException e) {
			e.printStackTrace();
		}
		
		return str;
	}

3,解析这个格式json数据(有集合的json数据)

{ 
"people": [
                { "firstName": "Brett", "lastName":"McLaughlin", "email": "aaaa" },
                { "firstName": "Jason", "lastName":"Hunter", "email": "bbbb"},
                { "firstName": "Elliotte", "lastName":"Harold", "email": "cccc" }
            ]
}
解析代码如下:
	public static List<String> parseJsonArray(String json){
		List<String> list = new ArrayList<String>();
		
		try {
			JSONObject jsonObject = new JSONObject(json);
			JSONArray jsonArray = jsonObject.getJSONArray("people");
			for(int i = 0; i < jsonArray.length(); i ++){
				String str = "";
				JSONObject jsonObject2 = jsonArray.getJSONObject(i);
				str = jsonObject2.getString("firstName");
				str = str + jsonObject2.getString("lastName");
				str = str +jsonObject2.getString("email");
				list.add(str);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return list;
	}


4,解析这个格式的json数据。(比较复杂的json数据)

{ "programmers": [
{ "firstName": "Brett", "lastName":"McLaughlin", "email": "aaaa" },
{ "firstName": "Jason", "lastName":"Hunter", "email": "bbbb" },
{ "firstName": "Elliotte", "lastName":"Harold", "email": "cccc" }
],
"authors": [
{ "firstName": "Isaac", "lastName": "Asimov", "genre": "science fiction" },
{ "firstName": "Tad", "lastName": "Williams", "genre": "fantasy" },
{ "firstName": "Frank", "lastName": "Peretti", "genre": "christian fiction" }
],
"musicians": [
{ "firstName": "Eric", "lastName": "Clapton", "instrument": "guitar" },
{ "firstName": "Sergei", "lastName": "Rachmaninoff", "instrument": "piano" }
] }
解析代码如下:

public static ArrayList<ArrayList<String>> parseComplexJson(String json){
		
		ArrayList<ArrayList<String>> lists = new ArrayList<ArrayList<String>>();
		
		try {
			JSONObject jsonObject = new JSONObject(json);
			JSONArray jsonArray = jsonObject.getJSONArray("programmers");
			
			ArrayList<String> list1 = new ArrayList<String>();
			for(int i = 0; i < jsonArray.length(); i ++ ){
				String str = "";
				JSONObject jsonObject2 = jsonArray.getJSONObject(i);
				str = jsonObject2.getString("firstName");
				str = str + jsonObject2.getString("lastName");
				str = str + jsonObject2.getString("email");
				list1.add(str);
			}
			lists.add(list1);
			
			ArrayList<String> list2 = new ArrayList<String>();
			JSONArray jsonArray2 = jsonObject.getJSONArray("authors");
			for(int i = 0; i < jsonArray2.length(); i ++){
				String str = "";
				JSONObject jsonObject2 = jsonArray2.getJSONObject(i);
				str =  jsonObject2.getString("firstName");
				str = str + jsonObject2.getString("lastName");
				str = str + jsonObject2.getString("genre");
				list2.add(str);
			}
			lists.add(list2);
			
			ArrayList<String> list3 = new ArrayList<String>();
			JSONArray jsonArray3 = jsonObject.getJSONArray("musicians");
			for(int i = 0; i < jsonArray3.length(); i ++){
				String str = "";
				JSONObject jsonObject2 = jsonArray3.getJSONObject(i);
				str = jsonObject2.getString("firstName");
				str = str + jsonObject2.getString("lastName");
				str = str + jsonObject2.getString("instrument");
				list3.add(str);
			}
			lists.add(list3);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		
		return lists;
	}

其中每个str都可以被具体的实体类的属性替换掉。我们可以依照这样的原理解析更为复杂的json数据。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值