Getting a specific hash-table from a list in racke

2019-08-03 01:59发布

问题:

So I'm trying to parse JSON from the Riot API, and I'm having a little trouble trying to get a specific hash-table. From what I understand, the api call is giving me a hash table, and in this table there is one other hash table, and in that table, is a large, variable amount of hash tables, each with another hash table. The table that I get

    {"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}},

and this goes on and on, with upto ~120 unique "id" tables that have the stats tables. I'm trying to access the {"id":0,"stats": table. It appears that the api returns this big block as a hash table, so I can use

(define ranked-summoner (hash-ref ranked-hash-json (string->symbol "champions")))

to access a LIST of the hash tables, but I'm not sure how to go about finding the "id":0 hash in that list.

my relevant code is as follows:

    (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) (define ranked-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" ranked-pentas) )

回答1:

Assuming the JSON string is parsed to a jsexpr like this:

(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))))))))

Then champions is a list of hashes. Therefore you'd need to examine each one like this:

(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))))

It would be more convenient if their API were to return champions as a hash where id is the key. But from what you're saying, they return it as a JSON array -- which Racket's json module parses as a Racket list -- so you have to examine the elements one by one.



回答2:

Write this function:

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

And then use it like this:

(champion-by-id 0 ranked-summoner)


标签: json hash racket