简体   繁体   English

如何解析不带引号的 JSON 字符串

[英]how to parse unquoted JSON string

I have a JSON string where neither the keys nor the values are quoted and I would like to transform it to a correctly formatted JSON.我有一个 JSON 字符串,其中既没有引用键也没有引用值,我想将其转换为格式正确的 JSON。

{basic:{0:{index:0, license:t, nameSID:n, image:"img_left", descriptionSID:t, category:r}}

Is there a Java library that could handle it?有没有可以处理它的Java库? I tried Jackson but it does not work.我试过杰克逊,但它不起作用。

You can use JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES with Jackson to allow unquoted field names:您可以将JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES与 Jackson 一起使用以允许不带引号的字段名称:

JsonFactory factory = new JsonFactory();
factory.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
JsonParser jp = factory.createJsonParser(new FileInputStream("content.json"));

Not sure if you got around to writing you own parser or not, but I did.不确定您是否开始编写自己的解析器,但我做到了。

https://github.com/ischumacher/rsjp https://github.com/ischumacher/rsjp

Here is example usage with your example JSON:以下是示例 JSON 的示例用法:

String str = "{basic:{0:{index:0, license:t, nameSID:n, image:\"img_left\", descriptionSID:t, category:r}}";
Map<String, Object> jso = Json.parseJSON(str);
System.out.println(jso);
System.out.println(Json.get(jso, "basic", "0", "image"));

Here is the output:这是输出:

{
   basic: 
   {
      0: 
      {
         index: 0, 
         license: t, 
         nameSID: n, 
         image: img_left, 
         descriptionSID: t, 
         category: r
      }
   }
}

img_left

I looked into how one might go about customizing Jackson to deal with non-quoted field values (vs. field names).我研究了如何自定义 Jackson 以处理未引用的字段值(与字段名称)。 Even though I ended-up writing a hack instead, I'm posting my breadcrumb trail here for others.尽管我最终写了一个 hack,但我还是在这里为其他人发布了我的面包屑路径。 My code spelunking was done in Jackson 2.7.2.我的代码探索是在 Jackson 2.7.2 中完成的。

Jackson Core comes with two concrete implementations of the JsonParser interface: Jackson Core 带有 JsonParser 接口的两个具体实现:

  • ReaderBasedJsonParser, a parser for character streams (encoding independent) ReaderBasedJsonParser,字符流解析器(独立于编码)
  • UTF8StreamJsonParser, a parser optimized for UTF-8 byte streams UTF8StreamJsonParser,一个针对 UTF-8 字节流优化的解析器

The code in these two classes is quite redundant, likely out of necessity.这两个类中的代码相当冗余,可能是出于必要。 Each class has a method which is called by nextToken() when an unexpected character is encountered.每个类都有一个在遇到意外字符时由 nextToken() 调用的方法。 ReaderBasedJsonParser's is named _handleOddValue() and UTF8StreamJsonParser's is _handleUnexpectedValue(). ReaderBasedJsonParser 的名为 _handleOddValue(),UTF8StreamJsonParser 的名为 _handleUnexpectedValue()。 Stuff like accepting "NaN" as a numeric value and allowing single-quoted string values happens in here.诸如接受“NaN”作为数值并允许单引号字符串值之类的事情发生在这里。

My plan (before I came to my senses and realized an awful hack would suffice for my short-term needs) was to subclass one/both of these parsers and override the methods above to handle unquoted string values.我的计划(在我意识到并意识到一个可怕的 hack 就足以满足我的短期需求之前)是对这些解析器中的一个/两个进行子类化,并覆盖上面的方法来处理未加引号的字符串值。 Because this method is called when the input stream is in the context of a field value (just after recognizing the colon), it should be possible to read forward until a comma or right curly brace is encountered and count everything read up to that point as a string value.因为当输入流处于字段值的上下文中时(就在识别冒号之后)会调用此方法,所以应该可以向前读取直到遇到逗号或右花括号并将到该点读取的所有内容计数为一个字符串值。 This code is tricky to write as it requires an understanding of Jackson's buffering strategy, the parser's architecture (the current pointer into the current buffer is an instance variable), etc.这段代码编写起来很棘手,因为它需要了解 Jackson 的缓冲策略、解析器的架构(当前指向当前缓冲区的指针是一个实例变量)等。

To make an ObjectMapper use this custom parser, one must subclass JsonFactory and override the _createParser() method with one which instantiates it.要使 ObjectMapper 使用此自定义解析器,必须继承 JsonFactory 并使用实例化它的方法覆盖 _createParser() 方法。 More work might be required to make both the regular and UTF-8 parser work correctly, although it's sufficient to force the use of the regular parser if performance isn't a concern.可能需要做更多工作才能使常规解析器和 UTF-8 解析器都能正常工作,但如果不考虑性能,强制使用常规解析器就足够了。 Then, an instance of this custom JsonFactory may be passed into ObjectMapper's constructor.然后,可以将这个自定义 JsonFactory 的实例传递给 ObjectMapper 的构造函数。

Hope this helps someone.希望这可以帮助某人。

You can use the nuget package unquotedJson , after installed, execute the following F# code, will get more severe quoted json format:你可以使用NuGet包unquotedJson ,安装好后,执行下面的F#代码,会得到更严重的引用JSON格式:

let x = """{
    0:{index:0, license:t, nameSID:n, image:"img:left", descriptionSID:t, category:r}
    }"""
let y = 
    x
    |> JSON.parse
    |> JSON.stringifyNormalJson

the y is: y是:

{"0":{"index":0,"license":"t","nameSID":"n","image":"img:left","descriptionSID":"t","category":"r"}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM