如何使用JSON-lib(http://json-lib.sourceforge.net/)将json字符串转换为Java Map?我可以转换为DynaBean:
JSONObject jsonObject = (JSONObject) JSONSerializer.toJSON( str );
DynaBean bean = (DynaBean) JSONSerializer.toJava( jsonObject );
Run Code Online (Sandbox Code Playgroud)
但我还没有找到一种直接转换为Java Map的方法
编辑:
我找到了解决方案:
JSONObject jsonObject = (JSONObject) JSONSerializer.toJSON( str );
Map<String, Object> myMap = (Map<String, Object>) JSONObject.toBean(jsonObject, Map.class)
Run Code Online (Sandbox Code Playgroud) 使用JSON-lib时JSONObject,如何阻止该put方法存储包含JSON作为JSON而不是转义字符串的String?
例如:
JSONObject obj = new JSONObject();
obj.put("jsonStringValue","{\"hello\":\"world\"}");
obj.put("naturalStringValue", "\"hello world\"");
System.out.println(obj.toString());
System.out.println(obj.getString("jsonStringValue"));
System.out.println(obj.getString("naturalStringValue"));
Run Code Online (Sandbox Code Playgroud)
打印:
{"jsonStringValue":{"hello":"world"},"naturalStringValue":"\"hello world\""}
{"hello":"world"}
"hello world"
Run Code Online (Sandbox Code Playgroud)
我希望它打印:
{"jsonStringValue":"{\"hello\":\"world\"}","naturalStringValue":"\"hello world\""}
{"hello":"world"}
"hello world"
Run Code Online (Sandbox Code Playgroud)
是的,我意识到这是令人讨厌的.但是,这支持JSON序列化管道,为了互操作性,这是预期的行为.在某些情况下,我们会序列化可能包含有效JSON的用户输入.我们不希望用户输入成为我们将所述输入序列化的JSON对象的一部分.
手动转义不起作用,因为它导致JSON-lib转义\字符:
JSONObject obj = new JSONObject();
obj.put("naturalJSONValue","{\"hello\":\"world\"}");
obj.put("escapedJSONValue", "{\\\"hello\\\":\\\"world\\\"}");
System.out.println(obj.toString());
System.out.println(obj.getString("naturalJSONValue"));
System.out.println(obj.getString("escapedJSONValue"));
Run Code Online (Sandbox Code Playgroud)
输出:
{"naturalJSONValue":{"hello":"world"},"escapedJSONValue":"{\\\"hello\\\":\\\"world\\\"}"}
{"hello":"world"}
{\"hello\":\"world\"}
Run Code Online (Sandbox Code Playgroud)
此时,任何启用手动选择性转义复杂JSON对象的变通方法都会完全否定首先使用JSON-lib的价值.
另外,我知道之前已经提出这个问题,但不幸的是我不能轻易接受它的答案.JSON-lib在我的项目的许多方面都是一个经常使用的依赖项,并且交换它将是一项艰巨的任务.我需要绝对肯定在我可以接受与Jackson,simple-json或Gson的交换之前,没有办法用JSON-lib实现这个目标.
我在java http://json-lib.sourceforge.net上使用JSON-lib库
我只是想添加一个看起来像JSON的简单字符串(但我不希望库自动发现它可能是json而只是将它视为字符串).看看图书馆的来源我找不到没有丑陋黑客的方法.
例:
JSONObject object = new JSONObject();
String chatMessageFromUser = "{\"dont\":\"treat it as json\"}";
object.put("myString", chatMessageFromUser);
Run Code Online (Sandbox Code Playgroud)
object.toString() 会给我们 {"myString":{"dont":"treat it as json"}}
而我想拥有 {"myString":"{\"dont\":\"treat it as json\"}"}
如何在不修改源代码的情况下实现它?我正在使用这段代码作为来自用户的聊天消息的传输 - 因此它适用于正常的聊天消息,但是当用户将输入JSON格式作为消息时,由于此处描述的JSON-lib的默认行为,它将破坏它.
我正在处理操纵"轻松"JSON数据的系统,其中包括shell样式的#行注释:
[
{
# Batman
"first-name": "Bruce",
"last-name": "Wayne"
},
{
# Superman
"first-name": "Clark",
"last-name": "Kent"
}
]
Run Code Online (Sandbox Code Playgroud)
我正在研究的系统部分使用json-lib - 我很惊讶地发现它容忍shell样式的注释 - 来解析JSON输入.
我需要从这些注释中提取一些额外的注释,但json-lib似乎只是放弃它们而不提供用于读取它们的API:
JSONObject map = (JSONObject)JSONSerializer.toJSON("{\n"+
" # Batman\n" + // note the shell-style # comment
" \"first-name\": \"Bruce\",\n" +
" \"last-name\": \"Wayne\"\n" +
"}");
System.out.println(map.toString());
/* <<'OUTPUT'
* {"first-name":"Bruce","last-name":"Wayne"}
* OUTPUT
* note the absence of the shell-style comment
*/
Run Code Online (Sandbox Code Playgroud)
这是有道理的,因为注释不是JSON规范的一部分,我很幸运json-lib在解析它们时不会窒息.
值得注意的是
在处理JSON输入时如何读取和解析这些注释?是否有一个库可以让我阅读它们并将它们与它们在JSON中的位置联系起来 - 我可以轻松地将Batman评论连接到"Bruce Wayne"条目吗?
我目前正在使用json-lib,但我愿意调查其他JSON库并同样开放使用扩展JSON的其他语言,例如YAML - 但我不确定这些工具是否允许我阅读和处理我的意见中的评论.
我是json解析的新手,我从请求中抓取了一个json字符串,现在我需要用java解析它.我正在使用json-lib.但我真的被困住了,因为我不熟悉它.我需要提取以下数据
1. name (hotel name)
2. starRating
3. geoPoint
Run Code Online (Sandbox Code Playgroud)
我使用了以下java代码,但它没有给我我需要的结果,请有人帮助我...
非常感谢!
java代码(s是我得到的json字符串)
JSONObject json = (JSONObject) JSONSerializer.toJSON(s);
JSONArray jarray = json.getJSONArray("hotels");
for(int i=0 ; i < jarray.size(); i++) {
System.out.println("jarray [" + i + "] --------" + jarray.getString(i));
}
Run Code Online (Sandbox Code Playgroud)
json我需要解析
[
{
"total": 250,
"offset": 0,
"requestID": "-btygi09oxfov",
"locationName": "Paris, France",
"locationLatitude": 48.86,
"locationLongitude": 2.34,
"cityCode": "PARIS_J_FR",
"hotels": [
{
"ypid": "YN10001x300073304",
"id": 56263,
"hotelRateIndicator": "2",
"name": "Renaissance Paris Vendome Hotel",
"brandCode": "69",
"addressLine1": "4 Rue du Mont-Thabor",
"city": "Paris", …Run Code Online (Sandbox Code Playgroud)