本文目錄一覽:
- 1、json的簡單介紹及基本使用
- 2、服務器端JSON數據或者xml數據如何設置
- 3、服務器端和客戶端進行json數據傳輸,json是不是也是通過http協議進行字節流傳輸的?
- 4、從服務器端獲取JSON格式的數據進行解析的問題,求幫助
- 5、怎麼解析從服務器返回的json
json的簡單介紹及基本使用
json是對象由鍵值對組成 jo={key:value,”t”:”haha”}這是一個json對象
使用也很簡單 json對象.key 可以取到value值如jo.t便可以取到對應的值
服務器端JSON數據或者xml數據如何設置
服務端打印出來字符串,客戶端解析
XML可以直接下載,然後解析
JSON就是一個字符串,放在TXT里就行
生成也很容易,PHP用file_put_contents可以生成TXT,json_encode生成JSON
服務器端和客戶端進行json數據傳輸,json是不是也是通過http協議進行字節流傳輸的?
先看一看json的定義: JSON(JavaScript Object Notation) 是一種輕量級的數據交換格式。
可見它只是一種數據格式,可以對其使用任何可行的傳輸協議。
但一般的網絡傳輸都使用http協議,
這和使用http協議傳輸視頻格式文件,音頻的道理是一致的。
json的傳輸相當於對字符串的傳輸。
所以:
服務器端和客戶端的json數據傳輸,可以而且最好使用http協議進行字節流傳輸,但不僅限於http協議。
從服務器端獲取JSON格式的數據進行解析的問題,求幫助
我們不返回json格式的字符串到客戶端呢然後再用js解析呢json字符串一定的弄個json文件存放嗎?
怎麼解析從服務器返回的json
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;ijsonArray.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。
原創文章,作者:KAMQ1,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/127992.html