[英]How to get each json object from a string in clojure?
I have a single string like with multiples json:我有一个像多个 json 一样的字符串:
{"operation": {"status": true, "limit": 100}}{"operation1": {"customer": "Jhon", "sum": 20, "time": "2019-02-13T10:00:00.000Z"}}{"operation1": {"customer": "Henry", "sum": 90, "time": "2019-02-13T11:00:00.000Z"}} {"operation": {"status": true, "limit": 100}}{"operation1": {"customer": "Jhon", "sum": 20, "time": "2019-02-13T10: 00:00.000Z"}}{"operation1": {"customer": "Henry", "sum": 90, "time": "2019-02-13T11:00:00.000Z"}}
I would like to get a list of json to be able to process each json as an individual object.我想获得一个 json 列表,以便能够将每个 json 作为一个单独的对象进行处理。
You can use json library with streaming support.您可以使用具有流支持的 json 库。 Eg
例如
(require '[cheshire.core :as json])
(->> "{\"operation\": {\"status\": true, \"limit\": 100}}{\"operation1\": {\"customer\": \"Jhon\", \"sum\": 20, \"time\": \"2019-02-13T10:00:00.000Z\"}}{\"operation1\": {\"customer\": \"Henry\", \"sum\": 90, \"time\": \"2019-02-13T11:00:00.000Z\"}}"
char-array
io/reader
json/parsed-seq
(take 2))
returns回报
({"operation" {"status" true, "limit" 100}}
{"operation1" {"customer" "Jhon", "sum" 20, "time" "2019-02-13T10:00:00.000Z"}})
I had to clean up your data.我不得不清理你的数据。 You may need to clarify where it is coming from so your question is more understandable (in context).
您可能需要澄清它的来源,以便您的问题更容易理解(在上下文中)。
(ns tst.demo.core
(:use demo.core tupelo.core tupelo.test)
(:require
[tupelo.core :as t]
[tupelo.string :as ts]))
; NOTE: I added square-brackets to wrap everyting in an array, and I also
; had to add commas between the array entries
(def data
"[ {'operation': {'status': true, 'limit': 100}},
{'operation1': {'customer': 'Jhon', 'sum': 20, 'time': '2019-02-13T10:00:00.000Z'}},
{'operation1': {'customer': 'Henry', 'sum': 90, 'time': '2019-02-13T11:00:00.000Z'}} ] " )
with result结果
(t/json->edn (ts/quotes->double data)) =>
({:operation {:status true, :limit 100}}
{:operation1
{:customer "Jhon", :sum 20, :time "2019-02-13T10:00:00.000Z"}}
{:operation1
{:customer "Henry", :sum 90, :time "2019-02-13T11:00:00.000Z"}})
This does not deserve to count as an answer beyond the helpful one by @rmcv , but it is too much for just a comment.除了@rmcv 提供的有用的答案之外,这不值得算作答案,但仅作评论就太过分了。 If useful, @rmcv should feel free to add this context to their answer and I can then delete this answer...
如果有用,@rmcv 可以随意将此上下文添加到他们的答案中,然后我可以删除此答案...
If you want to return the JSON keywords as Clojure keywords then a tweak to that answer is:如果您想将 JSON 关键字作为 Clojure 关键字返回,那么对该答案的调整是:
;; Emits map with JSON keys as Clojure keywords...
(-> "{\"operation\": {\"status\": true, \"limit\": 100}}{\"operation1\": {\"customer\": \"Jhon\", \"sum\": 20, \"time\": \"2019-02-13T10:00:00.000Z\"}{\"operation1\": {\"customer\": \"Henry\", \"sum\": 90, \"time\": \"2019-02-13T11:00:00.000Z\"}}"
char-array
io/reader
(json/parsed-seq true)
(->> (take 2)))
which returns返回
({:operation {:status true, :limit 100}}
{:operation1 {:customer "Jhon", :sum 20, :time "2019-02-13T10:00:00.000Z"}})
The true
argument to parsed-seq
just applies this function to the JSON keywords parsed-seq
的true
参数只是将此函数应用于 JSON 关键字
(fn [k] (keyword k))
and you can do more complex things with the keyword processing this way, ie (not so complex):并且您可以通过这种方式使用关键字处理来做更复杂的事情,即(不那么复杂):
(-> "{\"operation\": {\"status\": true, \"limit\": 100}}{\"operation1\": {\"customer\": \"Jhon\", \"sum\": 20, \"time\": \"2019-02-13T10:00:00.000Z\"}}{\"operation1\": {\"customer\": \"Henry\", \"sum\": 90, \"time\": \"2019-02-13T11:00:00.000Z\"}}"
char-array
io/reader
(chesire/parsed-seq (fn [k] (keyword (clojure.string/upper-case k))))
(->> (take 2)))
returns回报
({:OPERATION {:STATUS true, :LIMIT 100}}
{:OPERATION1 {:CUSTOMER "Jhon", :SUM 20, :TIME "2019-02-13T10:00:00.000Z"}})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.