I have a badly formatted JSON string that needs to be parsed. Keys of the JSON string are not wrapped in double quotes and string values are wrapped in single quotes instead of double quotes.

Example:

{ items: [
    { id: 1, name: 'test1' }, 
    { id: 2, name: 'test2' }
] }

What would be the better way to parse this JSON string?

1) Write a custom parser (how?)

2) Attempt to fix the JSON string by doing a couple of string_replace

One thing to note, the JSON string is pretty long (about 50kb) and contain quite many entries.

有帮助吗?

解决方案

Yaml is a super set (almost) of JSon and much more forgiving. It is much closer to what you have by default.

String s = "{ items: [\n" +
        "    { id: 1, name: 'test1' }, \n" +
        "    { id: 2, name: 'test2' }\n" +
        "] }";
Map map = (Map) new Yaml().load(s);
System.out.println(map);

prints

{items=[{id=1, name=test1}, {id=2, name=test2}]}

BTW This is how it dumps to Yaml by default.

System.out.println(new Yaml().dump(map));

prints

items:
- {id: 1, name: test1}
- {id: 2, name: test2}

I used snakeyaml. You can also bind the data to class(es)

其他提示

I think Antlr4 is the best tool for the job.

You can find here an example of a JSON grammar that you can easily modify for your purpose, e.g., the STRING lexeme can be modified from:

STRING :  '"' (ESC | ~["\\])* '"' ;

to:

STRING :  '\'' (ESC | ~["\\])* '\'' ;

and you can modify the pair rule to look like:

UNQUOTEDSTRING :  (ESC | ~["\\])* ;

pair:   UNQUOTEDSTRING ':' value ;

Then using a listener or a visitor you can convert your modified JSON to its correct version or parse it into objects directly.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top