从球拍列表中获取特定的哈希表?

时间:2015-04-13 03:09:00

标签: json hash racket

所以我试图从Riot 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")))

访问哈希表的LIST,但我不确定如何在该列表中查找"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")

(define(query-for-id summoner-id)   (定义ranking-stats(string-> url(string-replace api-id-request" SUMMONER_ID" summoner-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:~a \ n"排名 - pentas) )

2 个答案:

答案 0 :(得分:0)

假设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))))))))

然后championslist 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返回 - 其中,Racket的json模块将其解析为球拍list - 所以你必须逐个检查元素。

答案 1 :(得分:0)

写下这个功能:

(define (champion-by-id id champions)
    (findf (λ (champ)
              (eq? (hash-ref champ 'id) id)) champions))

然后像这样使用它:

(champion-by-id 0 ranked-summoner)