0

我有一个像倍数 json 一样的字符串:

{“操作”:{“状态”:true,“限制”:100}}{“操作1”:{“客户”:“Jhon”,“总和”:20,“时间”:“2019-02-13T10: 00:00.000Z"}}{"operation1": {"customer": "Henry", "sum": 90, "time": "2019-02-13T11:00:00.000Z"}}

我想获得一个 json 列表,以便能够将每个 json 作为一个单独的对象进行处理。

4

3 回答 3

3

您可以使用支持流式传输的 json 库。例如

(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))

返回

({"operation" {"status" true, "limit" 100}} 
 {"operation1" {"customer" "Jhon", "sum" 20, "time" "2019-02-13T10:00:00.000Z"}})
于 2019-11-28T15:46:34.210 回答
1

我不得不清理你的数据。您可能需要澄清它的来源,以便您的问题更容易理解(在上下文中)。

(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'}} ] " )

结果

(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"}})
于 2019-11-27T20:30:50.640 回答
0

除了@rmcv 提供的有用答案之外,这不应该算作一个答案,但仅仅发表评论就太过分了。如果有用,@rmcv 应该随时将此上下文添加到他们的答案中,然后我可以删除此答案...

如果您想将 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)))

返回

({:operation {:status true, :limit 100}} 
 {:operation1 {:customer "Jhon", :sum 20, :time "2019-02-13T10:00:00.000Z"}})

仅将此函数应用于 JSON 关键字的true参数parsed-seq

(fn [k] (keyword k))

并且您可以通过这种方式使用关键字处理来做更复杂的事情,即(不那么复杂):

(-> "{\"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)))

返回

({:OPERATION {:STATUS true, :LIMIT 100}} 
 {:OPERATION1 {:CUSTOMER "Jhon", :SUM 20, :TIME "2019-02-13T10:00:00.000Z"}})
于 2019-12-02T20:56:59.457 回答