| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1324 人关注过本帖
标题:请教JSON,如何取出其中嵌套的值
只看楼主 加入收藏
bhyycwk
Rank: 1
等 级:新手上路
帖 子:11
专家分:4
注 册:2012-2-27
结帖率:0
收藏
已结贴  问题点数:20 回复次数:7 
请教JSON,如何取出其中嵌套的值
TEXT TO cJsonstr noshow textme
{"errcode":0,"errmsg":"ok","answers":[{"id_type":1,"userid":"YaTou","report_values":[{"question_id":1,"multi_choice":[],"text":"张三","fileid":[]},{"question_id":2,"single_choice":2,"multi_choice":[],"fileid":[]},{"question_id":3,"single_choice":5,"multi_choice":[],"fileid":[]},{"question_id":4,"single_choice":2,"multi_choice":[],"fileid":[]},{"question_id":5,"multi_choice":[],"text":"35","fileid":[]},{"question_id":6,"multi_choice":[],"text":"11111111111","fileid":[]},{"question_id":7,"single_choice":2,"multi_choice":[],"fileid":[]},{"question_id":8,"single_choice":2,"multi_choice":[],"fileid":[]},{"question_id":9,"single_choice":2,"multi_choice":[],"fileid":[]},{"question_id":10,"single_choice":1,"multi_choice":[],"fileid":[]}],"report_time":1650402627},{"id_type":1,"userid":"LaoJia","report_values":[{"question_id":1,"multi_choice":[],"text":"李四","fileid":[]},{"question_id":2,"single_choice":2,"multi_choice":[],"fileid":[]},{"question_id":3,"single_choice":5,"multi_choice":[],"fileid":[]},{"question_id":4,"single_choice":2,"multi_choice":[],"fileid":[]},{"question_id":5,"multi_choice":[],"text":"64","fileid":[]},{"question_id":6,"multi_choice":[],"text":"2222222222","fileid":[]},{"question_id":7,"single_choice":2,"multi_choice":[],"fileid":[]},{"question_id":8,"single_choice":2,"multi_choice":[],"fileid":[]},{"question_id":9,"single_choice":2,"multi_choice":[],"fileid":[]},{"question_id":10,"single_choice":1,"multi_choice":[],"fileid":[]}],"report_time":1650404903}]}
ENDTEXT

m.cJsonStr=[var json=]+cJsonstr+[;]
m.oJs=CREATEOBJECT("MSScriptControl.ScriptControl")
m.oJs.Language="jscript"
m.oJs.addCode(m.cJsonStr)  
m.oSon=m.oJs.CodeObject.json.answers
FOR EACH m.SubItem IN m.oSon
?m.SubItem.userid    &&取出userid
?m.SubItem.report_time    &&取出时间
m.oSon1=m.SubItem.report_values   &&上报的数据

*!*    FOR EACH m.SubItem1 IN m.oSon1
*!*    ?m.SubItem1.text    &&取出姓名
*!*    EXIT
*!*    ENDFOR
ENDFOR
RELEASE m.oJs

report_values后面有10项,每项有四个,如何全部取出其中的值?
例如:question_id:1
      multi_choice:[]
      text:张三
      fileid:[]

      question_id:2
      ................
请指教。谢谢
搜索更多相关主题的帖子: FOR 取出 TEXT EXIT 嵌套 
2022-04-27 14:50
吹水佬
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:451
帖 子:10607
专家分:43182
注 册:2014-5-20
收藏
得分:5 
数据结构有问题,report_values后面有10项,每项四个属性名字不相同。
程序代码:
{
    "errcode": 0,
    "errmsg": "ok",
    "answers": [{
        "id_type": 1,
        "userid": "YaTou",
        "report_values": [{
            "question_id": 1,
            "multi_choice": [],
            "text": "张三",
            "fileid": []
        }, {
            "question_id": 2,
            "single_choice": 2,
            "multi_choice": [],
            "fileid": []
        }, {
            "question_id": 3,
            "single_choice": 5,
            "multi_choice": [],
            "fileid": []
        }, {
            "question_id": 4,
            "single_choice": 2,
            "multi_choice": [],
            "fileid": []
        }, {
            "question_id": 5,
            "multi_choice": [],
            "text": "35",
            "fileid": []
        }, {
            "question_id": 6,
            "multi_choice": [],
            "text": "11111111111",
            "fileid": []
        }, {
            "question_id": 7,
            "single_choice": 2,
            "multi_choice": [],
            "fileid": []
        }, {
            "question_id": 8,
            "single_choice": 2,
            "multi_choice": [],
            "fileid": []
        }, {
            "question_id": 9,
            "single_choice": 2,
            "multi_choice": [],
            "fileid": []
        }, {
            "question_id": 10,
            "single_choice": 1,
            "multi_choice": [],
            "fileid": []
        }],
        "report_time": 1650402627
    }, {
        "id_type": 1,
        "userid": "LaoJia",
        "report_values": [{
            "question_id": 1,
            "multi_choice": [],
            "text": "李四",
            "fileid": []
        }, {
            "question_id": 2,
            "single_choice": 2,
            "multi_choice": [],
            "fileid": []
        }, {
            "question_id": 3,
            "single_choice": 5,
            "multi_choice": [],
            "fileid": []
        }, {
            "question_id": 4,
            "single_choice": 2,
            "multi_choice": [],
            "fileid": []
        }, {
            "question_id": 5,
            "multi_choice": [],
            "text": "64",
            "fileid": []
        }, {
            "question_id": 6,
            "multi_choice": [],
            "text": "2222222222",
            "fileid": []
        }, {
            "question_id": 7,
            "single_choice": 2,
            "multi_choice": [],
            "fileid": []
        }, {
            "question_id": 8,
            "single_choice": 2,
            "multi_choice": [],
            "fileid": []
        }, {
            "question_id": 9,
            "single_choice": 2,
            "multi_choice": [],
            "fileid": []
        }, {
            "question_id": 10,
            "single_choice": 1,
            "multi_choice": [],
            "fileid": []
        }],
        "report_time": 1650404903
    }]
}
2022-04-27 19:31
sdta
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:江苏省连云港市
等 级:版主
威 望:335
帖 子:9841
专家分:27213
注 册:2012-2-5
收藏
得分:5 
程序代码:
CLEAR 
TEXT TO cJsonstr noshow textme
{"errcode":0,"errmsg":"ok","answers":[{"id_type":1,"userid":"YaTou","report_values":[{"question_id":1,"multi_choice":[],"text":"张三","fileid":[]},{"question_id":2,"single_choice":2,"multi_choice":[],"fileid":[]},{"question_id":3,"single_choice":5,"multi_choice":[],"fileid":[]},{"question_id":4,"single_choice":2,"multi_choice":[],"fileid":[]},{"question_id":5,"multi_choice":[],"text":"35","fileid":[]},{"question_id":6,"multi_choice":[],"text":"11111111111","fileid":[]},{"question_id":7,"single_choice":2,"multi_choice":[],"fileid":[]},{"question_id":8,"single_choice":2,"multi_choice":[],"fileid":[]},{"question_id":9,"single_choice":2,"multi_choice":[],"fileid":[]},{"question_id":10,"single_choice":1,"multi_choice":[],"fileid":[]}],"report_time":1650402627},{"id_type":1,"userid":"LaoJia","report_values":[{"question_id":1,"multi_choice":[],"text":"李四","fileid":[]},{"question_id":2,"single_choice":2,"multi_choice":[],"fileid":[]},{"question_id":3,"single_choice":5,"multi_choice":[],"fileid":[]},{"question_id":4,"single_choice":2,"multi_choice":[],"fileid":[]},{"question_id":5,"multi_choice":[],"text":"64","fileid":[]},{"question_id":6,"multi_choice":[],"text":"2222222222","fileid":[]},{"question_id":7,"single_choice":2,"multi_choice":[],"fileid":[]},{"question_id":8,"single_choice":2,"multi_choice":[],"fileid":[]},{"question_id":9,"single_choice":2,"multi_choice":[],"fileid":[]},{"question_id":10,"single_choice":1,"multi_choice":[],"fileid":[]}],"report_time":1650404903}]}
ENDTEXT
ALINES(laText, cJsonstr, 5, '"report_values":[')
FOR ln1 = 2 TO ALEN(laText)
    ALINES(laStr, laText[ln1], 5, "{", "},{", "}")
    FOR ln2 = 1 TO 10
        ALINES(la_Str, laStr[ln2], ":", ",")
        ? la_Str[1], la_Str[2], la_Str[3], la_Str[4], la_Str[5], la_Str[6], la_Str[7], la_Str[8]
    ENDFOR
ENDFOR

这样可以吗
图片附件: 游客没有浏览图片的权限,请 登录注册

坚守VFP最后的阵地
2022-04-27 19:32
bhyycwk
Rank: 1
等 级:新手上路
帖 子:11
专家分:4
注 册:2012-2-27
收藏
得分:0 
谢谢二位大佬,sdta版主给了我新的思路,JSON本来就是字符串,不一定必须使用JSON解析的。不过这个有个小小的遗憾,就是报文变化,需要重新编程,灵活性小一点,但是能解决问题。
2022-04-28 08:55
bhyycwk
Rank: 1
等 级:新手上路
帖 子:11
专家分:4
注 册:2012-2-27
收藏
得分:0 
我开始的帖子没有说明白,这个报文有两个人的数据,实际是有很多的。我原来想将数据取到一个表里,取值的方式是(举例):"userid"后面的"YaTou"值,然后是report_values后面的项,"question_id":1这个只是序号,不使用,需要的是他后面的"text"的值"张三","question_id": 7这个序号,需要的是 "single_choice"的值 2,"report_time"的值1650402627,然后将值写入一个数据表,再取第二个人的数据"userid"的值 "LaoJia",依次类推,键只作为数据表的字段名,值写入数据表。不知道是否有好的解决方法?谢谢了。使用sdta版主的方法也能解决。使用JSON解析,如何实现?

[此贴子已经被作者于2022-4-28 10:22编辑过]

2022-04-28 10:04
a57598587
Rank: 2
等 级:论坛游民
威 望:4
帖 子:29
专家分:29
注 册:2022-1-24
收藏
得分:5 
数据结构有问题,report_values后面有10项,每项四个属性名字不相同。

愚见:   将这10项的  所有属性 求最小公倍数, 然后 每一项中不包含的属性 添加该属性为空值  这样就好处理了  ,每一项作为一条记录 存储到数据表中 即可  

[此贴子已经被作者于2022-4-28 11:30编辑过]

2022-04-28 11:27
sdta
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:江苏省连云港市
等 级:版主
威 望:335
帖 子:9841
专家分:27213
注 册:2012-2-5
收藏
得分:0 
把问题说清楚,才能更好的解决问题,最后要达到什么目的

坚守VFP最后的阵地
2022-04-28 13:04
laowan001
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:66
帖 子:1088
专家分:2677
注 册:2015-12-30
收藏
得分:5 
木瓜大师的foxjson
https://blog.
2022-04-28 16:26
快速回复:请教JSON,如何取出其中嵌套的值
数据加载中...
 
   



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

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