本帖最后由 殇·愚 于 2019-9-16 00:22 编辑
最近在做一个对接API 接口的客户端, 需要解析API 下发的 json 格式数据.
先上一段获取到的API 数据:
{
"info": {
"page_rs": {
"entitle": "Products",
"banner": false,
"id": "45",
"parent_id": "0",
"site_id": "1",
"module": "24",
"cate": "70",
"title": "产品展示",
"nick_title": "",
"taxis": "50",
"status": "1",
"tpl_index": "",
"tpl_list": "",
"tpl_content": "",
"is_identifier": "0",
"ico": "images/ico/product.png",
"orderby": "l.sort DESC,l.dateline DESC,l.id DESC",
"alias_title": "产品名称",
"alias_note": "",
"psize": "12",
"uid": "0",
"identifier": "product",
"seo_title": "",
"seo_keywords": "",
"seo_desc": "",
"subtopics": "0",
"is_search": "1",
"is_tag": "1",
"is_biz": "1",
"is_userid": "0",
"is_tpl_content": "0",
"is_seo": "0",
"currency_id": "1",
"admin_note": "",
"hidden": "0",
"post_status": "0",
"comment_status": "1",
"post_tpl": "",
"etpl_admin": "",
"etpl_user": "",
"etpl_comment_admin": "",
"etpl_comment_user": "",
"is_attr": "1",
"tag": false,
"cate_multiple": "0",
"biz_attr": "1",
"freight": "1",
"list_fields": "",
"style": "",
"is_front": "0",
"is_api": "1",
"url": "https://www.xxxxxx.com/index.php?id=product"
},
"m_rs": {
"id": "24",
"title": "产品",
"status": "1",
"taxis": "40",
"note": "适用于电子商务中产品展示模型",
"layout": "hits,dateline,sort,thumb",
"mtype": "0",
"tbl": "list"
},
"cate_root": {
"id": "70",
"site_id": "1",
"parent_id": "0",
"status": "1",
"title": "产品分类",
"taxis": "20",
"tpl_list": "",
"tpl_content": "",
"psize": "0",
"seo_title": "",
"seo_keywords": "",
"seo_desc": "",
"identifier": "chanpinfenlei",
"tag": "",
"style": "",
"module_id": "0",
"url": "https://www.xxxxxx.com/index.php?id=product&cate=chanpinfenlei"
},
"pageid": "1",
"psize": "12",
"pageurl": "https://www.xxxxxx.com/api.php?id=product",
"total": "1",
"rslist": {
"2004": {
"id": "2004",
"parent_id": "0",
"cate_id": "582",
"module_id": "24",
"project_id": "45",
"site_id": "1",
"title": "测试品",
"dateline": "1567924670",
"sort": "0",
"status": "1",
"hidden": "0",
"hits": "51",
"tpl": "",
"seo_title": "",
"seo_keywords": "",
"seo_desc": "",
"tag": "",
"attr": "h,c,f",
"replydate": "0",
"user_id": "0",
"identifier": "",
"integral": "0",
"style": "",
"thumb": {
"id": "1403",
"cate_id": "1",
"folder": "res/201909/05/",
"name": "7fac75d1242a7ac5.jpg",
"ext": "jpg",
"filename": "res/201909/05/7fac75d1242a7ac5.jpg",
"ico": "res/_cache/_ico/14/1403.jpg",
"addtime": "1567668035",
"title": "460x0w",
"attr": {
"width": 460,
"height": 460
},
"note": "",
"session_id": "",
"user_id": "0",
"download": "0",
"admin_id": "1",
"mime_type": "image/jpeg",
"gd": {
"auto": "res/_cache/auto/14/1403.jpg",
"thumb": "res/_cache/thumb/14/1403.jpg"
}
},
"pictures": false,
"linkurl": "index.php?id=about",
"content": "<p>测试=125</p><p>拜托=145</p>",
"price": "10.0000",
"currency_id": "1",
"weight": "0",
"volume": "0",
"unit": "个",
"catelist": {
"582": {
"cate_id": "582",
"title": "小米",
"identifier": "xiaomi",
"url": "https://www.xxxxxx.com/api.php?id=product&cate=xiaomi"
}
},
"cate": {
"cate_id": "582",
"title": "小米",
"identifier": "xiaomi",
"url": "https://www.xxxxxx.com/api.php?id=product&cate=xiaomi"
},
"url": "https://www.xxxxxx.com/index.php?id=2004"
}
}
},
"status": 1,
"session_name": "PHPSESSION",
"session_val": "********************************"
}
再上 _json 的 UDF 文件, 我自己标注了备注.但好像理解有点问题. 或者没法解释清楚对应的功能. 导致没法正确使用. 这个是国外 AUTOIT 论坛里找到最完善的 json UDF , 但有些地方看不懂, 也没法把所有的数据变成对应的数组.. 尴尬...
; ============================================================================================================================
; File : Json.au3 (2018.12.29)
; Purpose : A Non-Strict JavaScript Object Notation (JSON) Parser UDF
; Author : Ward
; Dependency: BinaryCall.au3
; Website : http://www.json.org/index.html
;
; Source : jsmn.c
; Author : zserge
; Website : http://zserge.com/jsmn.html
;
; Source : json_string_encode.c, json_string_decode.c
; Author : Ward
; Jos - Added Json_Dump()
; TheXMan - Json_ObjGetItems and some Json_Dump Fixes.
; Jos - Changed Json_ObjGet() and Json_ObjExists() to allow for multilevel object in string.
; ============================================================================================================================
#cs 这段说明里的中文备注是我自己写的
; ============================================================================================================================
; Public Functions:
; Json_StringEncode($String, $Option = 0)
json 字符串编码 , 转换中文为 Unicode 格式
; Json_StringDecode($String)
获取到的 json 字符串解码为中文格式.
; Json_IsObject(ByRef $Object)
检测变量是否是json
; Json_IsNull(ByRef $Null)
检测变量是否为空
; Json_Encode($Data, $Option = 0, $Indent = Default, $ArraySep = Default, $ObjectSep = Default, $ColonSep = Default)
中文转编码为 Unicode 格式. 可以设置编码参数
; Json_Decode($Json, $InitTokenCount = 1000)
字段内容解码
; Json_ObjCreate()
创建json 变量
; Json_ObjPut(ByRef $Object, $Key, $Value)
写入指定键值的数据
; Json_ObjGet(ByRef $Object, $Key)
获取指定键值的数据
; Json_ObjDelete(ByRef $Object, $Key)
删除json 键名
; Json_ObjExists(ByRef $Object, $Key)
检测 json 的键值是否存在
; Json_ObjGetCount(ByRef $Object)
获取 json 的 总数量
; Json_ObjGetKeys(ByRef $Object)
获取 json 的 键名
; Json_ObjGetItems(ByRef $Object)
获取 json 的 值
; Json_ObjClear(ByRef $Object)
清空 json 的数据
; Json_Put(ByRef $Var, $Notation, $Data, $CheckExists = False)
提交新的数据到 json 里.
; Json_Get(ByRef $Var, $Notation)
获取 json 转储后的键值 对应的数据. 可以是 字段 或 数组.
$Json = '{"info":{"id":"23","group_id":"2","user":"admin","status":"1","regtime":"1438668082","email":"40782502@qq.com","mobile":"15818533979","code":"","avatar":"res/201907/19/782f85730e9c7fdf.jpg","fullname":"苏相锟","gender":"0","pca":{"title":["广东省","深圳市","龙华区"],"val":["广东省","深圳市","龙华区"],"type":"opt"},"address":"","wealth":{"integral":{"id":"1","title":"积分","val":3297,"unit":"点"},"wallet":{"id":"2","title":"钱包","val":"0.00","unit":"元"},"redbao":{"id":"5","title":"红包","val":"0.00","unit":"个"}}},"status":1,"session_name":"PHPSESSION","session_val":"********************************"}'
Json_dump($Json)
Local $Obj = JSon_Decode($Json)
_ArrayDisplay( Json_ObjGet( $Obj , ".info.pca.title" ) )
; Json_Dump($String)
JSON 获取后需要转储成为可以查询的KEY .
$Var 代表json 变量 , $Notation 为 Json_Dump 后的KEY .
; ============================================================================================================================
#ce
为了帖子方便观看. 我已经删除 UDF 的内容, 想要的可以下载附件哈. 或者到作者原帖查看下载.
UDF 原帖地址 : [url]https://www.autoitscript.com/forum/topic/148114-a-non-strict-json-udf-jsmn/?tab=comments#comment-1051873[/url]
现在. 我的问题是. 在我那段 json 里, 包含了 分类信息 字段 , 以及所属项目 信息字段 , 最后还有 分类里的数据列表字段. 就是带有编号 的数据.
"rslist": { "2004": { "id": "2004",
我通过 _json_dump() 命令可以看到以下输出
+-> .info.page_rs.entitle =Products
+-> .info.page_rs.banner =False
+-> .info.page_rs.id =45
+-> .info.page_rs.parent_id =0
+-> .info.page_rs.site_id =1
+-> .info.page_rs.module =24
+-> .info.page_rs.cate =70
+-> .info.page_rs.title =产品展示
+-> .info.page_rs.nick_title =
+-> .info.page_rs.taxis =50
+-> .info.page_rs.status =1
+-> .info.page_rs.tpl_index =
+-> .info.page_rs.tpl_list =
+-> .info.page_rs.tpl_content =
+-> .info.page_rs.is_identifier =0
+-> .info.page_rs.ico =images/ico/product.png
+-> .info.page_rs.orderby =l.sort DESC,l.dateline DESC,l.id DESC
+-> .info.page_rs.alias_title =产品名称
+-> .info.page_rs.alias_note =
+-> .info.page_rs.psize =12
+-> .info.page_rs.uid =0
+-> .info.page_rs.identifier =product
+-> .info.page_rs.seo_title =
+-> .info.page_rs.seo_keywords =
+-> .info.page_rs.seo_desc =
+-> .info.page_rs.subtopics =0
+-> .info.page_rs.is_search =1
+-> .info.page_rs.is_tag =1
+-> .info.page_rs.is_biz =1
+-> .info.page_rs.is_userid =0
+-> .info.page_rs.is_tpl_content =0
+-> .info.page_rs.is_seo =0
+-> .info.page_rs.currency_id =1
+-> .info.page_rs.admin_note =
+-> .info.page_rs.hidden =0
+-> .info.page_rs.post_status =0
+-> .info.page_rs.comment_status =1
+-> .info.page_rs.post_tpl =
+-> .info.page_rs.etpl_admin =
+-> .info.page_rs.etpl_user =
+-> .info.page_rs.etpl_comment_admin =
+-> .info.page_rs.etpl_comment_user =
+-> .info.page_rs.is_attr =1
+-> .info.page_rs.tag =False
+-> .info.page_rs.cate_multiple =0
+-> .info.page_rs.biz_attr =1
+-> .info.page_rs.freight =1
+-> .info.page_rs.list_fields =
+-> .info.page_rs.style =
+-> .info.page_rs.is_front =0
+-> .info.page_rs.is_api =1
+-> .info.page_rs.url =https://www.xxxxxx.com/index.php?id=product
+-> .info.m_rs.id =24
+-> .info.m_rs.title =产品
+-> .info.m_rs.status =1
+-> .info.m_rs.taxis =40
+-> .info.m_rs.note =适用于电子商务中产品展示模型
+-> .info.m_rs.layout =hits,dateline,sort,thumb
+-> .info.m_rs.mtype =0
+-> .info.m_rs.tbl =list
+-> .info.cate_root.id =70
+-> .info.cate_root.site_id =1
+-> .info.cate_root.parent_id =0
+-> .info.cate_root.status =1
+-> .info.cate_root.title =产品分类
+-> .info.cate_root.taxis =20
+-> .info.cate_root.tpl_list =
+-> .info.cate_root.tpl_content =
+-> .info.cate_root.psize =0
+-> .info.cate_root.seo_title =
+-> .info.cate_root.seo_keywords =
+-> .info.cate_root.seo_desc =
+-> .info.cate_root.identifier =chanpinfenlei
+-> .info.cate_root.tag =
+-> .info.cate_root.style =
+-> .info.cate_root.module_id =0
+-> .info.cate_root.url =https://www.xxxxxx.com/index.php?id=product&cate=chanpinfenlei
+-> .info.pageid =1
+-> .info.psize =12
+-> .info.pageurl =https://www.xxxxxx.com/api.php?id=product
+-> .info.total =1
+-> .info.rslist.2004.id =2004
+-> .info.rslist.2004.parent_id =0
+-> .info.rslist.2004.cate_id =582
+-> .info.rslist.2004.module_id =24
+-> .info.rslist.2004.project_id =45
+-> .info.rslist.2004.site_id =1
+-> .info.rslist.2004.title =测试品
+-> .info.rslist.2004.dateline =1567924670
+-> .info.rslist.2004.sort =0
+-> .info.rslist.2004.status =1
????????????????+-> .info.rslist.2004.hidden =0
+-> .info.rslist.2004.hits =51
+-> .info.rslist.2004.tpl =
+-> .info.rslist.2004.seo_title =
+-> .info.rslist.2004.seo_keywords =
+-> .info.rslist.2004.seo_desc =
+-> .info.rslist.2004.tag =
+-> .info.rslist.2004.attr =h,c,f
+-> .info.rslist.2004.replydate =0
+-> .info.rslist.2004.user_id =0
+-> .info.rslist.2004.identifier =
+-> .info.rslist.2004.integral =0
+-> .info.rslist.2004.style =
+-> .info.rslist.2004.thumb.id =1403
+-> .info.rslist.2004.thumb.cate_id =1
+-> .info.rslist.2004.thumb.folder =res/201909/05/
+-> .info.rslist.2004.thumb.name =7fac75d1242a7ac5.jpg
+-> .info.rslist.2004.thumb.ext =jpg
+-> .info.rslist.2004.thumb.filename =res/201909/05/7fac75d1242a7ac5.jpg
+-> .info.rslist.2004.thumb.ico =res/_cache/_ico/14/1403.jpg
+-> .info.rslist.2004.thumb.addtime =1567668035
+-> .info.rslist.2004.thumb.title =460x0w
+-> .info.rslist.2004.thumb.attr.width =460
+-> .info.rslist.2004.thumb.attr.height =460
+-> .info.rslist.2004.thumb.note =
+-> .info.rslist.2004.thumb.session_id =
+-> .info.rslist.2004.thumb.user_id =0
+-> .info.rslist.2004.thumb.download =0
+-> .info.rslist.2004.thumb.admin_id =1
+-> .info.rslist.2004.thumb.mime_type =image/jpeg
+-> .info.rslist.2004.thumb.gd.auto =res/_cache/auto/14/1403.jpg
+-> .info.rslist.2004.thumb.gd.thumb =res/_cache/thumb/14/1403.jpg
+-> .info.rslist.2004.pictures =False
+-> .info.rslist.2004.linkurl =index.php?id=about
+-> .info.rslist.2004.content =<p>测试=125</p><p>拜托=145</p>
+-> .info.rslist.2004.price =10.0000
+-> .info.rslist.2004.currency_id =1
+-> .info.rslist.2004.weight =0
+-> .info.rslist.2004.volume =0
+-> .info.rslist.2004.unit =个
+-> .info.rslist.2004.catelist.582.cate_id =582
+-> .info.rslist.2004.catelist.582.title =小米
+-> .info.rslist.2004.catelist.582.identifier =xiaomi
+-> .info.rslist.2004.catelist.582.url =https://www.xxxxxx.com/api.php?id=product&cate=xiaomi
+-> .info.rslist.2004.cate.cate_id =582
+-> .info.rslist.2004.cate.title =小米
+-> .info.rslist.2004.cate.identifier =xiaomi
+-> .info.rslist.2004.cate.url =https://www.xxxxxx.com/api.php?id=product&cate=xiaomi
+-> .info.rslist.2004.url =https://www.xxxxxx.com/index.php?id=2004
+-> .status =1
+-> .session_name =PHPSESSION
+-> .session_val =********************************
前面的字段前缀是固定的. 例如 .info.page_rs.id 之类的 我可以直接通过 _json_get() 获取到对应的数据.
但后面涉及到某个产品所属的ID : 2004 时 . .info.rslist.2004.id =2004 里 前面的 2004 没有其他地方可以获取到..
要如何才能先获取到 2004 这个数值. 我才能把列表数据读取出来. 或者这个UDF 里是否有生成数组的功能, 我可以通过数组读取.
我是有考虑修改这个UDF , 在里面遍历数据时增加一个写入数组的操作. 但一方面不清楚本身UDF 有没有这个功能. 研究了好几天源码一直没有完全看懂. 另一方面好像也
另外问个很基础的问题.. 如何才能建立一个没有限制的2D数组变量. 因为我不知道每次获取的json 里有多少的数组. 总不能 设置 $var[9999][999] 这样吧... 这问题好像很傻逼..可找了很久帮助文档都没有答案..
附件里我把从论坛那边下载的 _json 也上传了. 希望有兴趣的朋友可以一起把这个UDF 功能研究清楚, 一起完善起来, 做一个中文版本的说明.
|