I am trying to parse Json file which contains duplicate keys in java.I am using the method suggesteed in this answer Parsing a json which contains duplicate keys. The approach works fine if I hard-code the Json but if I read it from a file,only the last duplicate key is read.Please Help as I want to read the Json from a file.Thanks in advance. Code:
public class am {
public static void main(String args[]) throws Exception
{
JSONParser parser = new JSONParser();
Object obj = parser.parse(new FileReader("sample.json"));
JSONObject a=JSONObject.fromObject(obj);
JSONObject jsonObject=JSONObject.fromObject("{\n" +
" \"Data\": {\n" +
" \"A\": {\n" +
" \"B\": {\n" +
" \"C\": \"c\",\n" +
" \"D\": {}\n" +
" },\n" +
" \"E\": {\n" +
" \"F\": \"f\"\n" +
" },\n" +
" \"G\": {\n" +
" \"H\": \"h\"\n" +
" }\n" +
" },\"A\": {\n" +
" \"B\": {\n" +
" \"C\": \"x\",\n" +
" \"D\": {}\n" +
" },\n" +
" \"E\": {\n" +
" \"F\": \"y\"\n" +
" },\n" +
" \"G\": {\n" +
" \"H\": \"z\"\n" +
" }\n" +
" },\n" +
"
"\n" +
" }\n" +
"}");
System.out.println("Json objects are:::"+a);
System.out.println("Json objects are:::"+jsonObject);
}
}
Json File:
{
"Data": {
"A": {
"B": {
"C": "c",
"D": {}
},
"E": {
"F": "f"
},
"G": {
"H": "h"
}
},"A": {
"B": {
"C": "x",
"D": {}
},
"E": {
"F": "y"
},
"G": {
"H": "z"
}
},
}
}
Output:
Json objects are:::{"Data":{"A":{"B":{"C":"x","D":{}},"E":{"F":"y"},"G":{"H":"z"}}}}
Json objects are:::{"Data":{"A":[{"B":{"C":"c","D":{}},"E":{"F":"f"},"G":{"H":"h"}},{"B":{"C":"x","D":{}},"E":{"F":"y"},"G":{"H":"z"}}]}}