How do I parse JSON in Racket?

Electric Coffee

I can't seem to figure out the {documentation}, there aren't really any examples of parsing some simple JSON data, so I was wondering if anyone here could give me some examples to get started.

Chris Jester-Young

Here's a very simple example:

(require json)
(define x (string->jsexpr "{\"foo\": \"bar\", \"bar\": \"baz\"}"))
(for (((key val) (in-hash x)))
  (printf "~a = ~a~%" key val))

Here's how you can use it with a JSON-based API:

(require net/http-client json)
(define-values (status header response)
  (http-sendrecv "httpbin.org" "/ip" #:ssl? 'tls))
(define data (read-json response))
(printf "My IP address is ~a~%" (hash-ref data 'origin))

At the OP's request, here's how you can create a JSON value from a structure type:

(require json)
(struct person (first-name last-name age country))
(define (person->jsexpr p)
  (hasheq 'first-name (person-first-name p)
          'last-name (person-last-name p)
          'age (person-age p)
          'country (person-country p)))
(define cky (person "Chris" "Jester-Young" 33 "New Zealand"))
(jsexpr->string (person->jsexpr cky))

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类常见问题

How do I resolve ClassNotFoundException?

来自分类Dev

How to implement Racket-style struct in scheme?

来自分类Dev

How to parse a text file (CSV) into haskell so I can operate on it?

来自分类Dev

How do i sort my listview alphabetically?

来自分类Dev

How do I parse a JSONArray in Java with Json.simple?

来自分类Dev

How to parse values from a JSON file in Python

来自分类Dev

How do I disable object reference creation in the Newtonsoft JSON serializer?

来自分类Dev

How do I parse Json with an invalid character for a field name? e.g. { "file/folder": "/Shared/Salesforce/asdf.txt" } with Newtonsoft?

来自分类Dev

How to parse this JSON with GSON?

来自分类Dev

JSON.parse Dynamodb

来自分类Dev

JSON.parse Dynamodb

来自分类Dev

How to do a synchronous json ajax call with jquery

来自分类Dev

如何在Racket中解析JSON?

来自分类Dev

How to do Pagination for JSON data in PHP?

来自分类Dev

How do I parse my JSON with external middleware now that Express doesn't carry a body parser?

来自分类Dev

how to parse json using json_populate_recordset in postgres

来自分类Dev

How do I save a JSON file with four spaces indentation using JSON.NET?

来自分类Dev

How can I parse a JSON object with unknown keys?

来自分类Dev

How to parse nested JSON data structure

来自分类Dev

How should I parse this data?

来自分类Dev

How to parse following json in jquery ajax success function

来自分类Dev

How do I do a nested list (array) of schema references in json schema

来自分类Dev

How do I parse a RestSharp response into a class?

来自分类Dev

In this concrete case, how is "trace" supposed to work in Racket?

来自分类Dev

JSON PARSE无值

来自分类Dev

json.parse问题

来自分类Dev

com.parse.ParseRequest $ ParseRequestException:错误的json响应或文件上传期间发生I / O失败

来自分类Dev

Wiki上的JSon Parse

来自分类Dev

How do I get property value without knowing it's specific key in javascript using JSON

Related 相关文章

  1. 1

    How do I resolve ClassNotFoundException?

  2. 2

    How to implement Racket-style struct in scheme?

  3. 3

    How to parse a text file (CSV) into haskell so I can operate on it?

  4. 4

    How do i sort my listview alphabetically?

  5. 5

    How do I parse a JSONArray in Java with Json.simple?

  6. 6

    How to parse values from a JSON file in Python

  7. 7

    How do I disable object reference creation in the Newtonsoft JSON serializer?

  8. 8

    How do I parse Json with an invalid character for a field name? e.g. { "file/folder": "/Shared/Salesforce/asdf.txt" } with Newtonsoft?

  9. 9

    How to parse this JSON with GSON?

  10. 10

    JSON.parse Dynamodb

  11. 11

    JSON.parse Dynamodb

  12. 12

    How to do a synchronous json ajax call with jquery

  13. 13

    如何在Racket中解析JSON?

  14. 14

    How to do Pagination for JSON data in PHP?

  15. 15

    How do I parse my JSON with external middleware now that Express doesn't carry a body parser?

  16. 16

    how to parse json using json_populate_recordset in postgres

  17. 17

    How do I save a JSON file with four spaces indentation using JSON.NET?

  18. 18

    How can I parse a JSON object with unknown keys?

  19. 19

    How to parse nested JSON data structure

  20. 20

    How should I parse this data?

  21. 21

    How to parse following json in jquery ajax success function

  22. 22

    How do I do a nested list (array) of schema references in json schema

  23. 23

    How do I parse a RestSharp response into a class?

  24. 24

    In this concrete case, how is "trace" supposed to work in Racket?

  25. 25

    JSON PARSE无值

  26. 26

    json.parse问题

  27. 27

    com.parse.ParseRequest $ ParseRequestException:错误的json响应或文件上传期间发生I / O失败

  28. 28

    Wiki上的JSon Parse

  29. 29

    How do I get property value without knowing it's specific key in javascript using JSON

热门标签

归档