Racket字符串为文字



我正在做一个项目,从Riot Games API解析JSON,但遇到了麻烦。我对此还很陌生,所以请耐心等待:

API返回JSON,例如:

{"forcinit":{"id":35979437,"name":"F O R C I N it","profileIconId":576,"summonerLevel":30,"revisionDate":1427753158000}}

在我的代码中,我有以下内容:

#lang racket
(require 
 racket/gui/base
 net/url
 json
 racket/format
)
; --- Query API
(define api-request "https://na.api.pvp.net/api/lol/na/v1.4/summoner/by-name/SUMMONER_NAME?api_key=8864143b-a987-45a8-b49d-53c0a7677100")
(define (query-for-summoner name)
  (define summoner-request (string->url (string-replace api-request "SUMMONER_NAME" name)))
  ; Define request parameters, setup for output
  (define sh (get-pure-port summoner-request #:redirections 5))
  (define summoner-hash-str (port->string sh))
  (define summoner-hash-json (string->jsexpr summoner-hash-str))

  (define summoner (hash-ref summoner-hash-json name))
  ;I can't figure out how to make it so the "name" in the above line gets evaluted to the input, but read in as a literal 
  ;in order for it to correctly be identified for the hash. as of now, for example if i type in "Dyrus" in the gui box
  ; it says
  ;hash-ref: no value found for key
  ;key: "Dyrus"
  ;yet if i replace name with 'dyrus it works correctly.
  ;If you know of a way to take a variable and have racket read it as a literal, I would love to hear it.
  ;I've tried serching the documentation and googling but I can't seem to find what I'm looking for.
  (define summoner-id    (hash-ref summoner 'id))
  (define summoner-name  (hash-ref summoner 'name))
  (define summoner-icon  (hash-ref summoner 'profileIconId))
  (define summoner-level (hash-ref summoner 'summonerLevel))
  (printf "Results for: ~an" summoner-name)
  (printf "- ID: ~an" summoner-id)
  (printf "- Icon ID: ~an" summoner-icon)
  (printf "- Level: ~an" summoner-level)
)
; --- Build Frame
(define frame (new frame% 
 [label "League of Legends Statistics Tracker"]
 [width 300]
 [height 300]))
(send frame show #t)
(define msg (new message% 
 [parent frame]
 [label "Welcome to the LoL Stats Tracker."]))
(define sn-input (new text-field% 
 [parent frame]
 [label "Summoner Name: "]
 [init-value "omithegreat"]
 [callback (lambda(f ev)
             (send f get-value))
             ]))
(define submit-button (new button% 
 [parent frame]
 [label "Search"]
 [callback (lambda (button event)
             (let ([v (send sn-input get-value)])
               (query-for-summoner v))
             (send msg set-label "Searching for user..."))]))

我编辑掉了我的防暴API私钥;当我用我放在gui框中的任何名称替换name时,代码都能很好地工作,例如,如果我搜索dyrus,并且我替换"(define caller(hash-ref-caller hash-json-name))"具有(定义调用程序(hash-ref调用程序hash-json'dyrus))

它运行良好。有没有一种方法可以让我输入名称,然后将名称字符串转换为文字,就像带有'infront的相同字符串一样?

好吧,我认为Dan D.应该简单地发布他的评论作为答案:

我(也)认为你在寻找

string->symbol

(如果你支持这一点,你也应该支持Dan D.的评论,上面……)

最新更新