{Help:
[{"HelpOnName": "Merge",
"IsHelped": "Y",
"Comment":
[{"answers": "This is where the real answer test is",
"OtherDetails": "red",
"OtherHelpNeeded": "No"}]
}]
}
这是我需要解析的JSON,并将其作为KEY / Value放在map中。
问题是HELP
未被识别为JSONObject。你能建议如何解析并放入MAP
答案 0 :(得分:0)
根据JSON规范,所有键必须是字符串:
object = begin-object [ member *( value-separator member ) ] end-object
member = string name-separator value
如果您可以控制JSON的来源,则需要将Help
数组放在引号内:
{"Help":
[{"HelpOnName": "Merge",
"IsHelped": "Y",
"Comment":
[{"answers": "This is where the real answer test is",
"OtherDetails": "red",
"OtherHelpNeeded": "No"}]
}]
}
如果您无法控制来源 - 请从您的JSON中删除帮助:
[{"HelpOnName": "Merge",
"IsHelped": "Y",
"Comment":
[{"answers": "This is where the real answer test is",
"OtherDetails": "red",
"OtherHelpNeeded": "No"}]
}]
使用常规JSON解析器解析它。
答案 1 :(得分:0)
感谢您的帮助。是的,我可以做到这一点。所以现在我正在尝试下面的json,
[{" HelpOnName":" Merge", " IsHelped":" Y", "注释&#34 ;: [{"答案":"这是真正的答案测试的地方", " OtherDetails":" red", " OtherHelpNeeded":" No"}] }]
这篇评论中的是一个数组。所以我的问题是可以在运行时识别密钥,因为它是JSONObject和JSONArray,并以通用方法获取它们的值。因为我不想硬编码值来获取像
这样的值object.getString("answers");
object.getString("OtherDetails");
object.getString("OtherHelpNeeded");
所以现在在这种情况下,如果我改变我的json,那么我需要更改我的代码。因为我将这个json保存在属性文件中并在运行时获取它来解析它。