所以我想从防暴API解析JSON和我在试图让一个特定的哈希表有点麻烦。 从我的理解,API调用是给我一个哈希表,该表中有一个其他的哈希表,该表中,是哈希表的一个大的,可变的量,每一个与另一个哈希表。 我得到的表
{"summonerId":35979437,"modifyDate":1428864068000,"champions":
[{"id":40,"stats":
{"totalSessionsPlayed":2,"totalSessionsLost":1,"totalSessionsWon":1,"totalChampionKills":0,"totalDamageDealt":41909,"totalDamageTaken":27441,"mostChampionKillsPerSession":0,"totalMinionKills":22,"totalDoubleKills":0,"totalTripleKills":0,"totalQuadraKills":0,"totalPentaKills":0,"totalUnrealKills":0,"totalDeathsPerSession":4,"totalGoldEarned":15959,"mostSpellsCast":0,"totalTurretsKilled":0,"totalPhysicalDamageDealt":13865,"totalMagicDamageDealt":28042,"totalFirstBlood":0,"totalAssists":28,"maxChampionsKilled":0,"maxNumDeaths":4}},
{"id":111,"stats":
{"totalSessionsPlayed":8,"totalSessionsLost":6,"totalSessionsWon":2,"totalChampionKills":28,"totalDamageDealt":846416,"totalDamageTaken":248816,"mostChampionKillsPerSession":9,"totalMinionKills":337,"totalDoubleKills":2,"totalTripleKills":0,"totalQuadraKills":0,"totalPentaKills":0,"totalUnrealKills":0,"totalDeathsPerSession":45,"totalGoldEarned":80278,"mostSpellsCast":0,"totalTurretsKilled":4,"totalPhysicalDamageDealt":221463,"totalMagicDamageDealt":519678,"totalFirstBlood":0,"totalAssists":84,"maxChampionsKilled":9,"maxNumDeaths":10}},
这推移和,与高达120〜独特的“ID”有统计资料表的表。 我试图访问{"id":0,"stats":
表。 看来,API返回这个大块哈希表,所以我可以使用
(define ranked-summoner (hash-ref ranked-hash-json (string->symbol "champions")))
访问哈希表的列表,但我不知道如何去寻找"id":0
在该列表中的散列。
我的相关代码如下:
(define api-id-request "https://na.api.pvp.net/api/lol/na/v1.3/stats/by-summoner/35979437/ranked?api_key=8864143b-a987-45a8-b49d-53c0a7677100")
(定义(查询换ID召唤-ID)(定义排名-统计数据(与字符串> URL(字符串替换API-ID请求 “SUMMONER_ID” 召唤-ID)))
; Define request parameters for RANKED-STATS, setup for output
(define ranked (get-pure-port ranked-stats #:redirections 5))
(define ranked-hash-str (port->string ranked))
(define ranked-hash-json (string->jsexpr ranked-hash-str))
(define ranked-summoner (hash-ref ranked-hash-json (string->symbol "champions")))
;vvvv doesn't work, i need to find the hash that id=0, then grab the stats from there
(define ranked-champ-id (hash-ref (car ranked-summoner) (string->symbol "id")))
(define ranked-pentas (hash-ref ranked-champ-id (string->symbol "totalPentaKills")))
(printf的 “Pentakills:〜一\ n” 个排名-繁星))
假设JSON字符串被解析到一个jsexpr
这样的:
(define js
#hasheq((summonerId . 35979437)
(modifyDate . 1428864068000)
(champions
.
(#hasheq((id . 40)
(stats
.
#hasheq((totalSessionsPlayed . 2)
(totalSessionsLost . 1)
(totalSessionsWon . 1)
(totalChampionKills . 0)
(totalDamageDealt . 41909)
(totalDamageTaken . 27441)
(mostChampionKillsPerSession . 0)
(totalMinionKills . 22)
(totalDoubleKills . 0)
(totalTripleKills . 0)
(totalQuadraKills . 0)
(totalPentaKills . 0)
(totalUnrealKills . 0)
(totalDeathsPerSession . 4)
(totalGoldEarned . 15959)
(mostSpellsCast . 0)
(totalTurretsKilled . 0)
(totalPhysicalDamageDealt . 13865)
(totalMagicDamageDealt . 28042)
(totalFirstBlood . 0)
(totalAssists . 28)
(maxChampionsKilled . 0)
(maxNumDeaths . 4))))
#hasheq((id . 0)
(stats
.
#hasheq((totalSessionsPlayed . 8)
(totalSessionsLost . 6)
(totalSessionsWon . 2)
(totalChampionKills . 28)
(totalDamageDealt . 846416)
(totalDamageTaken . 248816)
(mostChampionKillsPerSession . 9)
(totalMinionKills . 337)
(totalDoubleKills . 2)
(totalTripleKills . 0)
(totalQuadraKills . 0)
(totalPentaKills . 0)
(totalUnrealKills . 0)
(totalDeathsPerSession . 45)
(totalGoldEarned . 80278)
(mostSpellsCast . 0)
(totalTurretsKilled . 4)
(totalPhysicalDamageDealt . 221463)
(totalMagicDamageDealt . 519678)
(totalFirstBlood . 0)
(totalAssists . 84)
(maxChampionsKilled . 9)
(maxNumDeaths . 10))))))))
然后, champions
是一个list
的hash
上课。 因此,你需要检查每一个这样的:
(define champs (hash-ref js 'champions))
(for/or ([champ (in-list champs)])
(cond [(= 0 (hash-ref champ 'id)) champ]
[else #f]))
;; =>
;; '#hasheq((id . 0)
;; (stats
;; .
;; #hasheq((totalSessionsPlayed . 8)
;; (totalSessionsLost . 6)
;; (totalSessionsWon . 2)
;; (totalChampionKills . 28)
;; (totalDamageDealt . 846416)
;; (totalDamageTaken . 248816)
;; (mostChampionKillsPerSession . 9)
;; (totalMinionKills . 337)
;; (totalDoubleKills . 2)
;; (totalTripleKills . 0)
;; (totalQuadraKills . 0)
;; (totalPentaKills . 0)
;; (totalUnrealKills . 0)
;; (totalDeathsPerSession . 45)
;; (totalGoldEarned . 80278)
;; (mostSpellsCast . 0)
;; (totalTurretsKilled . 4)
;; (totalPhysicalDamageDealt . 221463)
;; (totalMagicDamageDealt . 519678)
;; (totalFirstBlood . 0)
;; (totalAssists . 84)
;; (maxChampionsKilled . 9)
;; (maxNumDeaths . 10))))
如果他们的API被送回它会更方便champions
的散列结果,其中id
是关键。 但是,从你说的话,他们返回它作为一个JSON array
-这球拍的json
模块解析为一个球拍list
-所以你必须检查由一个要素之一。
写这个函数:
(define (champion-by-id id champions)
(findf (λ (champ)
(eq? (hash-ref champ 'id) id)) champions))
然后用它是这样的:
(champion-by-id 0 ranked-summoner)