本文目錄一覽:
求一個數據庫函數解析json 如下圖要求
數據庫只是用來存儲數據的,為什麼不把處理數據的部分放在後台?
在這裡你需要了解以下幾個知識點。
在sqlserver 中存儲json ,需要用字符串類型進行存儲,一般用nvarchar()或 varchar()進行存儲。
openJson:打開Json字符串
IsJson:判斷一個字符串是不是合法的Json格式。是返回1,否返回0,null返回null。
Json_Value:從Json字符串中提取值。
Json_Query:Json字符串中提取對象或數組。
Json_Modify:更新Json字符串中的屬性值,並返回更新的Json字符串
然後參考圖片的需求,獲取輸入key值中的value字段可以得到如下sql語句。
從JSON中獲取輸入B的JSON對象
SELECT JSON_QUERY(json, CONCAT(‘$.’,key)) INTO K_VAL;
從B的JSON對象中,獲取value字段的值
SELECT JSON_VALUE(K_VAL, ‘$.value’) INTO JSONVALUE;
最終我們的聲明的函數大概是:
CREATE FUNCTION PARSEJSON(json VARCHAR(2000), key VARCHAR(200))
RETURNS VARCHAR
BEGIN
declare JSONVALUE VARCHAR;
declare K_VAL VARCHAR;
SELECT JSON_QUERY(json, CONCAT(‘$.’,key)) INTO K_VAL;
SELECT JSON_VALUE(K_VAL, ‘$.value’) INTO JSONVALUE;
return JSONVALUE;
END
json數據解析出錯應該怎麼辦?
JSON數據解析錯誤處理辦法如下:
-JSONValue failed. Error is: Unescaped control character [0x0D]
這個錯誤就是JSON解析的時候String 的時候出現轉義字符。
對應用NSString 里的stringByReplacingOccurrencesOfString:@”\r”withString:@”” 取消掉轉義字符就OK那!
NSString *json_string1=[json_string stringByReplacingOccurrencesOfString:@”\r”withString:@””];
或者是在線工具生成的代碼,並不能保證百分百準確的。
如何解析返回的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。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/198288.html