本文目錄一覽:
- 1、JSON索引解析
- 2、json數據解析出錯應該怎麼辦?
- 3、JSON數據解析,
- 4、json的解析
JSON索引解析
存在,JSON中是否存在某個KEY,某些KEY,某些KEY的任意一個
存在某個KEY(TOP LEVEL)
‘{“a”:1, “b”:2}’::jsonb ? ‘b’
存在所有KEY
‘{“a”:1, “b”:2, “c”:3}’::jsonb ? array[‘b’, ‘c’]
存在任意KEY、元素
‘[“a”, “b”]’::jsonb ?| array[‘a’, ‘b’]
2、等值,JSON中是否存在指定的key:value對(支持嵌套JSON)
‘{“a”:1, “b”:2}’::jsonb @ ‘{“b”:2}’::jsonb
3、包含,JSON中某個路徑下的VALUE(數組)中,是否包含指定的所有元素。
postgres=# select jsonb ‘{“a”:1, “b”: {“c”:[1,2,3], “d”:[“k”,”y”,”z”]}, “d”:”kbc”}’ @ ‘{“b”:{“c”:[2,3]}}’;
?column?
———-
t
(1 row)
4、相交,JSON中某個路徑下的VALUE(數組)中,是否包含指定的任意元素。
postgres=# select jsonb ‘{“a”:1, “b”: {“c”:[1,2,3], “d”:[“k”,”y”,”z”]}, “d”:”kbc”}’ @ ‘{“b”:{“c”:[2]}}’
or
jsonb ‘{“a”:1, “b”: {“c”:[1,2,3], “d”:[“k”,”y”,”z”]}, “d”:”kbc”}’ @ ‘{“b”:{“c”:[3]}}’
;
?column?
———-
t
(1 row)
或(注意1,2,3需要雙引號,作為text類型存儲,因為操作符?| ?暫時只支持了text[],如果是numeric匹配不上)
postgres=# select jsonb ‘{“a”:1, “b”: {“c”:[“1″,”2″,”3”], “d”:[“k”,”y”,”z”]}, “d”:”kbc”}’ – ‘b’ – ‘c’ ? array[‘2′,’3′,’4’] ;
?column?
———-
f
(1 row)
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對象,然後直接獲取就可以了
字符串轉換為對象的方式為:JSON.parse(str)
轉換後的json對象設為jsonobj則想要獲取的值可通過如下方式獲取
var resp_value = jsonobj.result.cmd_resp;
然後彈窗看一下是否已經獲取到了值
alert(resp_value);
json的解析
var obj = {
“info”: {
“success”: true,
“code”: null,
“error”: null
},
“data”: [{
“id”: 1,
“name”: “測試用戶”,
“loginName”: “test”,
“password”: “test”,
“mobile1”: null,
“mobile2”: null,
“telephone”: null,
“email”: null,
“gender”: null,
“address”: null,
“removed”: 0
}, {
“id”: 21,
“name”: “研發團隊測試”,
“loginName”: “testTWW”,
“password”: “testTWW”,
“mobile1”: null,
“mobile2”: null,
“telephone”: null,
“email”: null,
“gender”: null,
“address”: null,
“removed”: 0
}]
};
var data = obj[“data”];
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/247316.html