i3geek.com
闫庚哲的个人博客

android 获取并解析json数据格式

利用系统自带com.org.json包解析json

示例json:

{
    "code": "1", 
    "msg": "查询成功", 
    "books": [
        {
            "bookid": 100836, 
            "bookname": "六年级英语"
        }, 
        {
            "bookid": 100836, 
            "bookname": "六年级英语"
        }
    ]
}

解析代码:

int code = jsonObject.getInt("code");
String msg = jsonObject.getString("msg");
JSONArray jsonArray = jsonObject.getJSONArray("books");
 // 循环获取Books
for (int i = 0; i < jsonArray.length(); i++) {
    JSONObject jo = jsonArray.getJSONObject(i);
    jo.getInt("bookid");
    jo.getString("bookname");
}

json获取代码:

try
// 获取json
{
	String path = "http://www.weather.com.cn/data/cityinfo/101010100.html";
	URL url = new URL(path);
	HttpURLConnection connection = (HttpURLConnection) url
			.openConnection();
	connection.setConnectTimeout(3000);
	connection.setRequestMethod("GET");
	connection.setDoInput(true);
	int code = connection.getResponseCode();
	if (code == 200)// 接收json
	{

		ByteArrayOutputStream outPutStream = new ByteArrayOutputStream();
		byte[] data = new byte[1024];
		int len = 0;
		try
		{
			while ((len = connection.getInputStream().read(data)) != -1)
			{
				outPutStream.write(data, 0, len);
			}
			jsonString = new String(outPutStream.toByteArray());

		} catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
} catch (Exception e)
{
	// TODO: handle exception
}

json数据格式解析我自己分为两种;

一种是普通的,一种是带有数组形式的;

普通形式的:

服务器端返回的json数据格式如下:

{"userbean":{"Uid":"100196","Showname":"\u75af\u72c2\u7684\u7334\u5b50","Avtar":null,"State":1}}

分析代码如下:

// TODO 状态处理 500 200 
int res = 0; 
res = httpClient.execute(httpPost).getStatusLine().getStatusCode(); 
if (res == 200) { 
/* 
* 当返回码为200时,做处理 
* 得到服务器端返回json数据,并做处理 
* */ 
HttpResponse httpResponse = httpClient.execute(httpPost); 
StringBuilder builder = new StringBuilder(); 
BufferedReader bufferedReader2 = new BufferedReader( 
new InputStreamReader(httpResponse.getEntity().getContent())); 
String str2 = ""; 
for (String s = bufferedReader2.readLine(); s != null; s = bufferedReader2 
.readLine()) { 
builder.append(s); 
} 
Log.i("cat", ">>>>>>" + builder.toString());

JSONObject jsonObject = new JSONObject(builder.toString()) 
.getJSONObject("userbean");

String Uid; 
String Showname; 
String Avtar; 
String State;

Uid = jsonObject.getString("Uid"); 
Showname = jsonObject.getString("Showname"); 
Avtar = jsonObject.getString("Avtar"); 
State = jsonObject.getString("State");

 带数组形式的:

服务器端返回的数据格式为:

{"calendar": 
{"calendarlist": 
[ 
{"calendar_id":"1705","title":"(\u4eb2\u5b50)ddssd","category_name":"\u9ed8\u8ba4\u5206\u7c7b","showtime":"1288927800","endshowtime":"1288931400","allDay":false}, 
{"calendar_id":"1706","title":"(\u65c5\u884c)","category_name":"\u9ed8\u8ba4\u5206\u7c7b","showtime":"1288933200","endshowtime":"1288936800","allDay":false} 
] 
} 
}

分析代码如下:

// TODO 状态处理 500 200 
int res = 0; 
res = httpClient.execute(httpPost).getStatusLine().getStatusCode(); 
if (res == 200) { 
/* 
* 当返回码为200时,做处理 
* 得到服务器端返回json数据,并做处理 
* */ 
HttpResponse httpResponse = httpClient.execute(httpPost); 
StringBuilder builder = new StringBuilder(); 
BufferedReader bufferedReader2 = new BufferedReader( 
new InputStreamReader(httpResponse.getEntity().getContent())); 
String str2 = ""; 
for (String s = bufferedReader2.readLine(); s != null; s = bufferedReader2 
.readLine()) { 
builder.append(s); 
} 
Log.i("cat", ">>>>>>" + builder.toString()); 
/** 
* 这里需要分析服务器回传的json格式数据, 
*/ 
JSONObject jsonObject = new JSONObject(builder.toString()) 
.getJSONObject("calendar"); 
JSONArray jsonArray = jsonObject.getJSONArray("calendarlist"); 
for(int i=0;i<jsonArray.length();i++){ 
JSONObject jsonObject2 = (JSONObject)jsonArray.opt(i); 
CalendarInfo calendarInfo = new CalendarInfo(); 
calendarInfo.setCalendar_id(jsonObject2.getString("calendar_id")); 
calendarInfo.setTitle(jsonObject2.getString("title")); 
calendarInfo.setCategory_name(jsonObject2.getString("category_name")); 
calendarInfo.setShowtime(jsonObject2.getString("showtime")); 
calendarInfo.setEndtime(jsonObject2.getString("endshowtime")); 
calendarInfo.setAllDay(jsonObject2.getBoolean("allDay")); 
calendarInfos.add(calendarInfo); 
}

 总结,普通形式的只需用JSONObject ,带数组形式的需要使用JSONArray 将其变成一个list。

赞(0)
未经允许不得转载:爱上极客 » android 获取并解析json数据格式
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址