提取 json 中的有效字段

本文探讨了如何在JSON数据中提取特定元素,特别是当数据以数组形式开始时。通过使用JsonDataSource和jsonpath,尝试访问元素但未成功。示例JSON数据包含赛马比赛详情,目标是根据输入参数获取“horseId”,“trainerId”和“ownerColours”。解决方案是采用SPL处理JSON,能够高效地获取所需信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

【问题】

A valid form of a JSON document begins as an Array element. How do you access any element using the JsonDataSource “jsonpath” to query it? I have tried using the standard $[0] for the top level fields and as “$” for creating my subdataset but neither works… when I place the array under a standard element it works fin,e but that is not the format of our JSON.

My JSON loks a bit like this:

[  {      “race”: {          “raceId”: “1.33.1141109.2”,          “startDate”: “2014-11-09T13:15:00.000Z”,          “raceClassification”: {             “classification”: “Novices’”         },          “raceType”: {            “key”: “H”         },          “raceClass”: 4,          “course”:          {            “courseId”: “1.33”         },          “meetingId”: “1.33.1141109”       },       “numberOfRunners”: 2,       “runners”: [         {             “horseId”: “1.00387464”,             “trainer”: {                “trainerId”: “1.00034060”            },             “ownerColours”: “Maroon, pink sleeves, dark blue cap.”          },          {            “horseId”: “1.00373620”,             “trainer”: {                “trainerId”: “1.00010997”            },             “ownerColours”: “Black, emerald green cross of lorraine, striped sleeves.”          }       ]    },    {      “race”: {          “raceId”: “1.33.1141109.3”,          “startDate”: “2014-11-09T13:45:00.000Z”,          “raceClassification”:  {             “classification”: “Handicap”         },          “raceType”: {            “key”: “C”         },          “raceClass”: 4,          “course”: {            “courseId”: “1.33”         },          “meetingId”: “1.33.1141109”       },       “numberOfRunners”: 2,       “runners”: [         {             “horseId”: “1.00297339”,             “trainer”:             {                “trainerId”: “1.00000577”            },             “ownerColours”: “Maroon and light blue (quartered), maroon sleeves.”          },          {            “horseId”: “1.00333030”,             “trainer”:             {                “trainerId”: “1.00000065”,            },             “ownerColours”: “Emerald green, yellow hoops, white cap.”          }       ]    } ]

输入参数(比如 1 或 2),从 json 文件中取出数组中指定成员的三个字段:horseId, trainerId,ownerColours。比如输入 1,结果应该是:

1.003874641.00034060Maroon, pink sleeves, dark blue cap.

1.003736201.00010997Black, emerald green cross of lorraine, striped sleeves.

【回答】

使用 SPL 处理 json 数据后可以快速获取所需要的信息,代码如下:

A
1=file(“d:\\data.json”).read()
2=json(A1)
3=A2(which).runners
4=A3.new(horseId,trainer.trainerId:trainerId,ownerColours)

运行结果:

A1:读取 json 串

A2:解析 json 形成序表

A3:指定数组成员,which 是参数名本例中可以取 1 或者 2,获得节点二维表

A4:提取所需要的三个字段,生成新序表,其中 trainerId 要从下级取。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值