[英]Unusual syntax JSON parse problem String (Clojure)
I have a long clojure String (java.lang.String) representing a JSON. 我有一个很长的Clojure字符串(java.lang.String),它表示一个JSON。 It has "normal" looking syntax like so:
它具有“正常”外观的语法,如下所示:
"{ a:1, b:"hello" }"
The keys do not have quotes around them. 键周围没有引号。
All the libraries I have looked at such as clojure.data.json want this syntax in order to parse into an object: 我查看过的所有库(例如clojure.data.json)都希望使用此语法来解析为一个对象:
"{\"a\":1,\"b\":"hello"}"
Is there a way to parse the top syntax into json or clj objects? 有没有一种方法可以将顶级语法解析为json或clj对象? Any help is appreciated.
任何帮助表示赞赏。 Thank you.
谢谢。
Depending on your level of desperation, the Groovy JSON Slurper can read this "JSON" in the LAX
setting (which allows those attributes, comments, ...) 根据您的绝望程度,Groovy JSON Slurper可以在
LAX
设置(允许这些属性,注释等)中读取此“ JSON”
Add the following dep: 添加以下内容:
[org.codehaus.groovy/groovy-json "2.5.6"]
Then you can run: 然后,您可以运行:
user=> (import [groovy.json JsonSlurper JsonParserType])
groovy.json.JsonParserType
user=> (def parser (doto (JsonSlurper.) (.setType JsonParserType/LAX)))
#'user/parser
user=> (def data (.parseText parser "{ a:1, b:\"hello\" }"))
#'user/data
user=> data
{"a" 1, "b" "hello"}
The link from @jas includes a comment that "it looks like YAML" (if the colons are followed by a space). @jas的链接包含“看起来像YAML”的注释(如果冒号后跟一个空格)。
I have integrated the new snakeyaml-engine library into the Tupelo Library . 我已经将新的snakeyaml-engine库集成到Tupelo库中 。 API docs are here .
API文档在这里 。 The answer is now super-simple:
答案现在非常简单:
(ns tst.demo.core
(:use demo.core tupelo.core tupelo.test)
(:require [tupelo.parse.yaml :as yaml] ))
(dotest
(let [data-1 "{ a: 1, b: 'Hello World' }"]
(is= (yaml/parse data-1) {:a 1, :b "Hello World"})))
Given project.clj
给定
project.clj
[io.forward/yaml "1.0.9"]
this works: 这有效:
(ns tst.demo.core
(:require [yaml.core :as yaml]) )
(def data-1 "{ a: 1, b: \"Hello World\" }" )
(let [result-raw (yaml/parse-string data-1)
result (into {} result-raw)]
with result 结果
----------------------------------
Clojure 1.10.0 Java 10.0.1
----------------------------------
Testing tst.demo.core
result-raw => #ordered/map ([:a 1] [:b "Hello World"])
result => {:a 1, :b "Hello World"}
Unfortunately it (currently) fails under Java 11 because a dependent library needs a type hint. 不幸的是,它(当前)在Java 11下失败了,因为从属库需要类型提示。 You can work around this by fixing up your
project.clj
: 您可以通过修复
project.clj
来解决此问题:
:dependencies [
[io.forward/yaml "1.0.9" :exclusions [org.flatland/ordered
org.yaml/snakeyaml] ]
[org.yaml/snakeyaml "1.23"]
[org.flatland/ordered "1.5.7"]
[org.clojure/clojure "1.10.0"]
I filed an issue with the io.forward/yaml
project to update their dependencies. 我对
io.forward/yaml
项目提出了问题,以更新其依赖性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.