Json 从racket中的列表中获取特定的哈希表?

Json 从racket中的列表中获取特定的哈希表?,json,hash,racket,Json,Hash,Racket,因此,我试图从riotapi解析JSON,但在获取特定的哈希表时遇到了一些困难。据我所知,api调用给了我一个哈希表,在这个表中有另一个哈希表,在这个表中有大量的、可变数量的哈希表,每个都有另一个哈希表。我得到的那张桌子 {"summonerId":35979437,"modifyDate":1428864068000,"champions": [{"id":40,"stats": {"totalSessionsPlayed":2,"totalSession

因此,我试图从riotapi解析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将这个大块作为哈希表返回,因此我可以使用

(定义排名召唤师(hash-ref排名hash-json(字符串->符号“冠军”))

访问哈希表列表,但我不确定如何在该列表中查找
“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请求“召唤者id”召唤者id)))

(printf“Pentakills:~a\n”排名的penta)
)

假设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))))))))
(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))))
(champion-by-id 0 ranked-summoner)
然后
champions
是一个
list
hash
es。因此,您需要像这样检查每一个:

(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))))))))
(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))))
(champion-by-id 0 ranked-summoner)

如果他们的API将
champions
作为散列返回,其中
id
是关键,那就更方便了。但是从你所说的,他们将其作为JSON
数组返回-,哪个Racket的
JSON
模块作为Racket
列表解析-,所以你必须逐个检查元素。

写这篇文章声明:

(define (champion-by-id id champions)
    (findf (λ (champ)
              (eq? (hash-ref champ 'id) id)) champions))
然后像这样使用它:

(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))))))))
(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))))
(champion-by-id 0 ranked-summoner)