I am trying to parse a string which in JSON format only that keys are not enclosed in quotes. I can very well parse this string in Javascript, but can not find a Java API which will help me parse this. All the APIs I tried assumes strict JSON format.
Can anyone suggest a library which has an option to parse this, or a whole new approach to the problem (say like use regex instead) ?
I am trying to parse a string which in JSON format only that keys are not enclosed in quotes. I can very well parse this string in Javascript, but can not find a Java API which will help me parse this. All the APIs I tried assumes strict JSON format.
Can anyone suggest a library which has an option to parse this, or a whole new approach to the problem (say like use regex instead) ?
Share Improve this question asked Jun 7, 2010 at 23:22 NishanNishan 2,8714 gold badges28 silver badges36 bronze badges 2- json/js.html says "JSON is a subset of the object literal notation of JavaScript". So I guess my question now bees, is there a Java parser for object literal notation of Javascript. – Nishan Commented Jul 13, 2010 at 20:39
- 1 In 2021, it's definitely mon to have "relaxed JSON", where keys don't need to be surrounded with quotes, single quotes can be used around values (instead of double-quotes), etc. oleg.fi/relaxed-json was helpful for me. – Ryan Commented Jan 28, 2021 at 14:50
4 Answers
Reset to default 2Here's a solution in coffeescript using the underscore library. If you're not using that you can replace _.foldl with a for loop.
parseNonStrictJson = (value) ->
inQuote = false
correctQuotes = (memo, nextChar) ->
insertQuote =
(inQuote and not /[a-z0-9_"]/.test nextChar) or
(!inQuote and /[a-z_]/.test nextChar)
inQuote = (inQuote != (insertQuote or nextChar == '"') )
memo + (if insertQuote then '"' else '') + nextChar
valueWithQuotes = _.foldl(value + '\n', correctQuotes, "")
JSON.parse(valueWithQuotes)
And the same in javascript:
function parseNonStrictJson(value) {
var correctQuotes, inQuote, valueWithQuotes;
inQuote = false;
correctQuotes = function(memo, nextChar) {
var insertQuote;
insertQuote = (inQuote && !/[a-z0-9_"]/.test(nextChar)) || (!inQuote && /[a-z_]/.test(nextChar));
inQuote = inQuote !== (insertQuote || nextChar === '"');
return memo + (insertQuote ? '"' : '') + nextChar;
};
valueWithQuotes = _.foldl(value + '\n', correctQuotes, "");
return JSON.parse(valueWithQuotes);
};
If the keys aren't enclosed in quotes then it's not JSON.
You should either hack this yourself or find someone who did it already.
Also, there's no such thing as non-strict json. There's only 1 version of JSON and it's strict.
Personally, you could use a state pattern and add your quotes. Unless I am wrong, the state pattern would read in character by character and set flags to indicate whether we are within a double quote condition and whether our double quotes are "quoted" with a backslash. Using this, and that variable names don't start with a number, you could add the quotes while streaming it, then send it on it's way.
You might use eval:
var parsed = eval(json)
Be careful because eval
could also run code so you must be sure that you know what you are parsing.
There is also a node module called jsonic that parses non stirct JSON.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745215463a4616991.html
评论列表(0条)