views:

46

answers:

2

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

+2  A: 

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.

Luca Matteis
I am now trying to hack on org.json code to remove this restriction. Just wanted to see if anyone has a nicer way around this.
Nishan
Also, wonder why javascript does not complain about keys not enclosed in quotes.
Nishan
Because JSON is not JavaScript object notation, even though that's what it stands for.
Eli Grey
@Nishan: you should also wonder why you can have functions as values in JavaScript... Anyway you could pass your object to a browser and use JSON.stringify on json.org to make the keys have quotes again. You still need a JS interpreter though (the browser).
Luca Matteis
A: 

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.

Dr. Zim
This is pretty much what json.org's implementation does. I was able to solve this by extending org.json.JSONTokener class.
Nishan