Reputation: 6832
My JSON file's content is :
{
"MJ" : "Michael Jordan",
"KB" : "Kobe Bryant",
"KG" : "Kevin Garnet"
}
Now it is easy to convert this file (or string) to a Java class (e.g: java.util.HashMap
) if I use Gson or Jackson or JsonSimple or another Java/JSON third-party library. E.g., I can use Gson like this:
//this returns the string above
String jsonString = TestReader.getStrFromJSonFile();
Gson gson = new Gson();
Type type = new TypeToken<HashMap<String, String>>() {}.getType();
HashMap<String, String> map = gson.fromJson(jsonString, type);
But,if my JSON file contains comments, most of the third-party libraries will not work. e.g.:
{ //Bulls "MJ" : "Michael Jordan", /*Lakers*/ "KB" : "Kobe Bryant", //Boston Celtics "KG" : "Kevin Garnet" }
Now I'm even confused after googling and stackoverflowing a lot. I have two questions:
Can a JSON file really contain any comments? I think it can because I see a lot of this in JSON files everyday. But >>> (links)
If it can, how can I solve my problem?
Upvotes: 3
Views: 1797
Reputation: 35463
Jackson supports comments if you enable that feature on the parser you use:
Upvotes: 4