2015-03-31 47 views
0

我正在做一個項目,我從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: ~a\n" summoner-name) 
    (printf "- ID: ~a\n" summoner-id) 
    (printf "- Icon ID: ~a\n" summoner-icon) 
    (printf "- Level: ~a\n" 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私鑰;例如,如果我搜索了dyrus,並且替換了「(定義召喚者(hash-ref summoner-hash-json name))」 與 (定義召喚者(hash-ref summoner-hash-json'dyrus))

它工作正常。 有沒有辦法讓我可以輸入名稱,然後將名稱字符串轉換爲文字,就像是一個'infront?

+7

'string-> symbol' – 2015-03-31 04:35:27

+0

這不是直接關係到你的問題,而是在你的提交按鈕回調中,你應該設置標籤*之前*你'查詢召喚師'而不是之後。 – 2015-03-31 07:08:27

+0

要挑剔,可能是你無法搜索答案的原因,「文字」是隻存在於源代碼中的東西;它是一個值的字面表示('1'和'「dyson」'也是文字)。在運行時無法將值轉換爲文字。你正在尋找的概念是「符號」。 – molbdnilo 2015-03-31 09:25:02

回答

3

好吧,我覺得丹D.應該簡單地已經張貼作爲回答他的評論:

我(還)以爲你正在尋找

string->symbol 

(如果您給予好評,你應該也贊同Dan D.的評論,以上....)