问题
I'm doing a project where I'm parsing JSON from the Riot Games API, and I'm having trouble. I'm pretty new to this, so bear with me:
The API returns JSON, for example:
{"forcinit":{"id":35979437,"name":"F O R C I N it","profileIconId":576,"summonerLevel":30,"revisionDate":1427753158000}}
in my code, I have the following:
#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..."))]))
I edited out my riot API private key; The code works fine when I replace name with whatever name I put in the gui box, for example if I searched for dyrus, and i replace "(define summoner (hash-ref summoner-hash-json name))" with (define summoner (hash-ref summoner-hash-json 'dyrus))
it works fine. Is there a way so I can input the name, then turn the name string into a literal, like the same string with a ' infront?
回答1:
Okay, I think Dan D. should simply have posted his comment as an answer:
I (also) think you're looking for
string->symbol
(If you upvote this, you should also upvote Dan D.'s comment, above....)
来源:https://stackoverflow.com/questions/29360497/racket-string-to-literal