-1
 {Help:  
   [{"HelpOnName": "Merge", 
     "IsHelped": "Y",
     "Comment": 
   [{"answers": "This is where the real answer test is",
     "OtherDetails": "red",
     "OtherHelpNeeded": "No"}]
   }]
 }

This is the JSON I need to parse and put it in map as KEY / Value.

Problem is HELP is not been identified as JSONObject. Can you suggest how to parse and put in MAP

cнŝdk
  • 31,391
  • 7
  • 56
  • 78
ami
  • 35
  • 7

2 Answers2

0

By JSON specification all keys must be strings:

object = begin-object [ member *( value-separator member ) ] end-object
member = string name-separator value

If you can control source of the JSON you need to put Help array inside the quotes:

{"Help":  
   [{"HelpOnName": "Merge", 
     "IsHelped": "Y",
     "Comment": 
   [{"answers": "This is where the real answer test is",
     "OtherDetails": "red",
     "OtherHelpNeeded": "No"}]
   }]
 }

If you can't control source - remove Help from your JSON:

   [{"HelpOnName": "Merge", 
     "IsHelped": "Y",
     "Comment": 
   [{"answers": "This is where the real answer test is",
     "OtherDetails": "red",
     "OtherHelpNeeded": "No"}]
   }]

And parse it with regular JSON parser.

Dmitry Poroh
  • 3,705
  • 20
  • 34
  • My json will have HELP and this format of json cant be changed. By useing above json i am now only getting the HELP as a key and rest all is skipped. – ami Jul 13 '15 at 12:16
  • and also how we can identify that the element coming is an JSONObject or JSONArray on run time? i dont want to hard code the values to retrieve the array or object – ami Jul 13 '15 at 12:19
  • @ami If Help is not the single field you can patch that output to be compatible with JSON format. I'm not familiar with java but I strongly believe that you can do it with regular expression. – Dmitry Poroh Jul 13 '15 at 12:54
0

Thanks for your help. yes i can do that. so now i am trying with below json,

[{"HelpOnName": "Merge", "IsHelped": "Y", "Comment": [{"answers": "This is where the real answer test is", "OtherDetails": "red", "OtherHelpNeeded": "No"}] }]

in this comments is an array. so my question is is it possible to identify the key as it is JSONObject and JSONArray at runtime and fetch their values in a generic approach. as i dont want to hard code values to fetch the values like

object.getString("answers");
object.getString("OtherDetails");
object.getString("OtherHelpNeeded");

so now in this case if i change my json then i need to change my code. as i am keeping this json in a property file and getting it at runtime to parse it.

ami
  • 35
  • 7