| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 630 人关注过本帖, 1 人收藏
标题:老师,请教JSON读取的记录的问题
只看楼主 加入收藏
laibinhua
Rank: 2
等 级:论坛游民
威 望:1
帖 子:295
专家分:38
注 册:2010-12-23
结帖率:58.02%
收藏(1)
 问题点数:0 回复次数:9 
老师,请教JSON读取的记录的问题
【读取方法这样报错,读取不到数据field_code,请问原因:】
m.cJsonStr3=[var json=]+cTxt+[;]
m.oJs3=CREATEOBJECT("MSScriptControl.ScriptControl")
m.oJs3.Language="jscript"
m.oJs3.addCode(m.cJsonStr3)
m.oSon3=m.oJs3.CodeObject.json.result.field_data_list
FOR EACH m.SubItem IN m.oSon3
    l_user1=m.SubItem.field_code
ENDFOR

【cTxt字符串如下:】  
{"errcode":0,"errmsg":"ok","result":[{"corp_id":"ding0a2c55ff26c8472da1320dcb25e91351","field_data_list":[{"field_code":"sys01-positionLevel","field_name":"岗位职级","field_value_list":[{"item_index":0}],"group_id":"sys01"},{"field_code":"sys00-tel","field_name":"分机号","field_value_list":[{"item_index":0}],"group_id":"sys00"},{"field_code":"sys05-nowContractEndTime","field_name":"现合同到期日","field_value_list":[{"item_index":0}],"group_id":"sys05"},{"field_code":"sys01-regularTime","field_name":"实际转正日期","field_value_list":[{"item_index":0}],"group_id":"sys01"},{"field_code":"sys00-reportManager","field_name":"直属主管","field_value_list":[{"item_index":0}],"group_id":"sys00"},{"field_code":"sys00-remark","field_name":"备注","field_value_list":[{"item_index":0}],"group_id":"sys00"},{"field_code":"sys00-position","field_name":"职位","field_value_list":[{"item_index":0}],"group_id":"sys00"},{"field_code":"sys00-name","field_name":"姓名","field_value_list":[{"item_index":0,"label":"叶波","value":"叶波"}],"group_id":"sys00"},{"field_code":"sys00-email","field_name":"邮箱","field_value_list":[{"item_index":0}],"group_id":"sys00"},{"field_code":"sys00-dept","field_name":"部门","field_value_list":[{"item_index":0}],"group_id":"sys00"},{"field_code":"sys00-entryAge","field_name":"司龄(系统计算)","field_value_list":[{"item_index":0,"label":"","value":""}],"group_id":"sys00"},{"field_code":"sys01-employeeType","field_name":"员工类型","field_value_list":[{"item_index":0,"label":"全职","value":"1"}],"group_id":"sys01"},{"field_code":"sys00-deptIds","field_name":"部门id","field_value_list":[{"item_index":0}],"group_id":"sys00"},{"field_code":"sys05-contractPeriodType","field_name":"合同期限","field_value_list":[{"item_index":0}],"group_id":"sys05"},{"field_code":"sys01-probationPeriodType","field_name":"试用期","field_value_list":[{"item_index":0}],"group_id":"sys01"},{"field_code":"sys02-age","field_name":"年龄(系统计算)","field_value_list":[{"item_index":0,"label":"","value":""}],"group_id":"sys02"},{"field_code":"sys05-contractRenewCount","field_name":"续签次数","field_value_list":[{"item_index":0}],"group_id":"sys05"},{"field_code":"sys00-workPlace","field_name":"办公地点","field_value_list":[{"item_index":0}],"group_id":"sys00"},{"field_code":"sys00-mainDept","field_name":"主部门","field_value_list":[{"item_index":0}],"group_id":"sys00"},{"field_code":"sys05-contractType","field_name":"合同类型","field_value_list":[{"item_index":0}],"group_id":"sys05"},{"field_code":"sys05-firstContractEndTime","field_name":"首次合同到期日","field_value_list":[{"item_index":0}],"group_id":"sys05"},{"field_code":"sys02-workAge","field_name":"工龄(系统计算)","field_value_list":[{"item_index":0,"label":"","value":""}],"group_id":"sys02"},{"field_code":"sys05-contractCompanyName","field_name":"合同公司","field_value_list":[{"item_index":0}],"group_id":"sys05"},{"field_code":"sys00-mainDeptId","field_name":"主部门id","field_value_list":[{"item_index":0}],"group_id":"sys00"},{"field_code":"sys05-firstContractStartTime","field_name":"首次合同起始日","field_value_list":[{"item_index":0}],"group_id":"sys05"},{"field_code":"sys05-nowContractStartTime","field_name":"现合同起始日","field_value_list":[{"item_index":0}],"group_id":"sys05"},{"field_code":"sys00-confirmJoinTime","field_name":"入职时间","field_value_list":[{"item_index":0}],"group_id":"sys00"},{"field_code":"sys01-planRegularTime","field_name":"计划转正日期","field_value_list":[{"item_index":0}],"group_id":"sys01"},{"field_code":"sys-authRealName","field_name":"实人认证","field_value_list":[{"item_index":0,"label":"未授权","value":"1"}],"group_id":"sys"},{"field_code":"sys00-mobile","field_name":"手机号","field_value_list":[{"item_index":0,"label":"+86-18580640160","value":"+86-18580640160"}],"group_id":"sys00"},{"field_code":"sys02-joinWorkingTime","field_name":"首次参加工作时间","field_value_list":[{"item_index":0}],"group_id":"sys02"},{"field_code":"sys01-employeeStatus","field_name":"员工状态","field_value_list":[{"item_index":0,"label":"离职","value":"4"}],"group_id":"sys01"},{"field_code":"sys00-jobNumber","field_name":"工号","field_value_list":[{"item_index":0,"label":"A00003","value":"A00003"}],"group_id":"sys00"}],"userid":"6823515547694188"}],"success":true,"request_id":"16kcgex4u02q4"}

[此贴子已经被作者于2023-6-26 14:26编辑过]

搜索更多相关主题的帖子: 统计 value label 记录 读取 
2023-06-26 14:19
吹水佬
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:451
帖 子:10607
专家分:43186
注 册:2014-5-20
收藏(2)
得分:0 
result 和 field_data_list 是数组,要用数组对象获取数组元素。
试用MSHTML的IHTMLDOCUMENT接口htmlfile对象载入JS来解析json
图片附件: 游客没有浏览图片的权限,请 登录注册

程序代码:
TEXT TO cJson TEXTMERGE NOSHOW PRETEXT 15
{"errcode":0,"errmsg":"ok","result":[{"corp_id":"ding0a2c55ff26c8472da1320dcb25e91351","field_data_list":[{"field_code":"sys01-positionLevel","field_name":"岗位职级","field_value_list":[{"item_index":0}],"group_id":"sys01"},{"field_code":"sys00-tel","field_name":"分机号","field_value_list":[{"item_index":0}],"group_id":"sys00"},{"field_code":"sys05-nowContractEndTime","field_name":"现合同到期日","field_value_list":[{"item_index":0}],"group_id":"sys05"},{"field_code":"sys01-regularTime","field_name":"实际转正日期","field_value_list":[{"item_index":0}],"group_id":"sys01"},{"field_code":"sys00-reportManager","field_name":"直属主管","field_value_list":[{"item_index":0}],"group_id":"sys00"},{"field_code":"sys00-remark","field_name":"备注","field_value_list":[{"item_index":0}],"group_id":"sys00"},{"field_code":"sys00-position","field_name":"职位","field_value_list":[{"item_index":0}],"group_id":"sys00"},{"field_code":"sys00-name","field_name":"姓名","field_value_list":[{"item_index":0,"label":"叶波","value":"叶波"}],"group_id":"sys00"},{"field_code":"sys00-email","field_name":"邮箱","field_value_list":[{"item_index":0}],"group_id":"sys00"},{"field_code":"sys00-dept","field_name":"部门","field_value_list":[{"item_index":0}],"group_id":"sys00"},{"field_code":"sys00-entryAge","field_name":"司龄(系统计算)","field_value_list":[{"item_index":0,"label":"","value":""}],"group_id":"sys00"},{"field_code":"sys01-employeeType","field_name":"员工类型","field_value_list":[{"item_index":0,"label":"全职","value":"1"}],"group_id":"sys01"},{"field_code":"sys00-deptIds","field_name":"部门id","field_value_list":[{"item_index":0}],"group_id":"sys00"},{"field_code":"sys05-contractPeriodType","field_name":"合同期限","field_value_list":[{"item_index":0}],"group_id":"sys05"},{"field_code":"sys01-probationPeriodType","field_name":"试用期","field_value_list":[{"item_index":0}],"group_id":"sys01"},{"field_code":"sys02-age","field_name":"年龄(系统计算)","field_value_list":[{"item_index":0,"label":"","value":""}],"group_id":"sys02"},{"field_code":"sys05-contractRenewCount","field_name":"续签次数","field_value_list":[{"item_index":0}],"group_id":"sys05"},{"field_code":"sys00-workPlace","field_name":"办公地点","field_value_list":[{"item_index":0}],"group_id":"sys00"},{"field_code":"sys00-mainDept","field_name":"主部门","field_value_list":[{"item_index":0}],"group_id":"sys00"},{"field_code":"sys05-contractType","field_name":"合同类型","field_value_list":[{"item_index":0}],"group_id":"sys05"},{"field_code":"sys05-firstContractEndTime","field_name":"首次合同到期日","field_value_list":[{"item_index":0}],"group_id":"sys05"},{"field_code":"sys02-workAge","field_name":"工龄(系统计算)","field_value_list":[{"item_index":0,"label":"","value":""}],"group_id":"sys02"},{"field_code":"sys05-contractCompanyName","field_name":"合同公司","field_value_list":[{"item_index":0}],"group_id":"sys05"},{"field_code":"sys00-mainDeptId","field_name":"主部门id","field_value_list":[{"item_index":0}],"group_id":"sys00"},{"field_code":"sys05-firstContractStartTime","field_name":"首次合同起始日","field_value_list":[{"item_index":0}],"group_id":"sys05"},{"field_code":"sys05-nowContractStartTime","field_name":"现合同起始日","field_value_list":[{"item_index":0}],"group_id":"sys05"},{"field_code":"sys00-confirmJoinTime","field_name":"入职时间","field_value_list":[{"item_index":0}],"group_id":"sys00"},{"field_code":"sys01-planRegularTime","field_name":"计划转正日期","field_value_list":[{"item_index":0}],"group_id":"sys01"},{"field_code":"sys-authRealName","field_name":"实人认证","field_value_list":[{"item_index":0,"label":"未授权","value":"1"}],"group_id":"sys"},{"field_code":"sys00-mobile","field_name":"手机号","field_value_list":[{"item_index":0,"label":"+86-18580640160","value":"+86-18580640160"}],"group_id":"sys00"},{"field_code":"sys02-joinWorkingTime","field_name":"首次参加工作时间","field_value_list":[{"item_index":0}],"group_id":"sys02"},{"field_code":"sys01-employeeStatus","field_name":"员工状态","field_value_list":[{"item_index":0,"label":"离职","value":"4"}],"group_id":"sys01"},{"field_code":"sys00-jobNumber","field_name":"工号","field_value_list":[{"item_index":0,"label":"A00003","value":"A00003"}],"group_id":"sys00"}],"userid":"6823515547694188"}],"success":true,"request_id":"16kcgex4u02q4"}
ENDTEXT
TEXT TO jsCode TEXTMERGE NOSHOW PRETEXT 7
    <script>
        var json  = <<cJson>>;
        var result = function(nResult){
            return json.result[nResult];
        }
        var field_data_list = function(nResult, nField_data_list){
            return json.result[nResult].field_data_list[nField_data_list];
        }
    </script>
ENDTEXT
doc = CREATEOBJECT("htmlfile")
doc.write(jsCode)
window = doc.parentWindow
json = window.json
? json.errmsg, json.request_id
FOR i=0 TO window.result(0).field_data_list.length - 1
    field_data_list = window.field_data_list(0,i)
    ? field_data_list.field_code, field_data_list.field_name
ENDFOR



收到的鲜花
  • kangss2023-06-27 15:53 送鲜花  1朵   附言:这个方法好
2023-06-26 15:34
schtg
Rank: 12Rank: 12Rank: 12
来 自:Usa
等 级:贵宾
威 望:67
帖 子:1730
专家分:3324
注 册:2012-2-29
收藏
得分:0 
回复 2楼 吹水佬
谢谢!
2023-06-26 18:03
厨师王德榜
Rank: 18Rank: 18Rank: 18Rank: 18Rank: 18
等 级:贵宾
威 望:199
帖 子:989
专家分:4966
注 册:2013-2-16
收藏
得分:0 
用 你熟悉的 MSScriptControl.ScriptControl 对象读取也是可以的,但是由于他是多重嵌套结构,所以需要双重循环遍历数组.
参考代码:
程序代码:
CLEAR 
* 利用MSScriptControl.ScriptControl类,读取嵌套的json文本一例.

LOCAL jsonf as String ,cJsonStr3
LOCAL oJs3 as Object ,oSon4 as object ,oSon3 as Object
LOCAL ii as Integer

jsonf = "c:\XIDE\Prg\cc.json"  && 这是含有嵌套的json文件
* 我们要读取他的_result.field_data_list.所有的field_code(共33个)
CREATE CURSOR field_codeLst (id i ,field_code c(100)) 
ii = 1

m.cJsonStr3=[var json=] + FILETOSTR(jsonf) + [;]
m.oJs3=CREATEOBJECT("MSScriptControl.ScriptControl")
m.oJs3.Language="jscript"
m.oJs3.addCode(m.cJsonStr3)
m.oSon3=m.oJs3.CodeObject.json.result  &&  子级
FOR EACH m.SubItem IN m.oSon3
    oSon4 = m.SubItem.field_data_list  && 孙级
    FOR EACH m.SubItem2 IN m.oSon4    
        l_user1=m.SubItem2.field_code
        INSERT INTO field_codeLst values(ii, l_user1)
        ii = ii + 1
    ENDFOR 
ENDFOR

SELECT field_codeLst
BROWSE 
2023-06-27 11:13
csyx
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:火星
等 级:版主
威 望:37
帖 子:707
专家分:2833
注 册:2018-3-13
收藏
得分:0 
以下是引用厨师王德榜在2023-6-27 11:13:00的发言:

用 你熟悉的 MSScriptControl.ScriptControl 对象读取也是可以的,但是由于他是多重嵌套结构,所以需要双重循环遍历数组.

我也一直这样处理,但没找到做成一个通用解析器的方法
主要是没法知道返回的 json 对象有哪些属性,命令窗口中, . 操作分明可以弹出该对象的属性列表,可 vfp 的 Amember 函数却无法得到该对象的属性列表,加上参数 3 也不行。谁有好办法解决?

这家伙很懒,啥也没留下
2023-06-27 11:22
吹水佬
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:451
帖 子:10607
专家分:43186
注 册:2014-5-20
收藏(1)
得分:0 
回复 5楼 csyx
遍历 { name:value, ... } 对象用 for(变量 in 对象名字){ ... }
程序代码:
TEXT TO jsCode TEXTMERGE NOSHOW PRETEXT 7
    var andy = {"name":"pink","age":18,"sex":"男"};
    function fun(obj){
        var ret=""
        for (var o in obj){ 
            ret = ret + "," + o + ":" + obj[o];
        } 
        return ret.substr(1);
    }      
ENDTEXT
script = CREATEOBJECT("ScriptControl")
script.Language = "JavaScript"
script.addCode(jsCode)
?script.run("fun",script.CodeObject.andy)
2023-06-27 16:31
csyx
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:火星
等 级:版主
威 望:37
帖 子:707
专家分:2833
注 册:2018-3-13
收藏
得分:0 
以下是引用吹水佬在2023-6-27 16:31:18的发言:
遍历 { name:value, ... } 对象用 for(变量 in 对象名字){ ... }

谢吹版,这也不错,好过我现在的另写dll取key名列表的方案
只是不明白 vfp 的命令窗口是如何枚举出所有 key 名的

这家伙很懒,啥也没留下
2023-06-27 17:36
吹水佬
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:451
帖 子:10607
专家分:43186
注 册:2014-5-20
收藏
得分:0 
以下是引用csyx在2023-6-27 17:36:33的发言:

只是不明白 vfp 的命令窗口是如何枚举出所有 key 名的

vfp可以解析JavaScript暴露给VFP的对象,否则vfp也获取不到对象name的value。
但AMEMBERS()可能不支持JavaScript自定义对象(属ECMAScript)。
另:JavaScript中的对象3种:自定义对象 、内置对象、 浏览器对象。
2023-06-27 21:03
laibinhua
Rank: 2
等 级:论坛游民
威 望:1
帖 子:295
专家分:38
注 册:2010-12-23
收藏
得分:0 
回复 4楼 厨师王德榜
谢谢大佬回复
2023-06-28 10:48
laibinhua
Rank: 2
等 级:论坛游民
威 望:1
帖 子:295
专家分:38
注 册:2010-12-23
收藏
得分:0 
回复 4楼 厨师王德榜
谢谢你的方法真好我这边好用

[此贴子已经被作者于2023-6-28 11:06编辑过]

2023-06-28 11:01
快速回复:老师,请教JSON读取的记录的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.030359 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved