我有以下(工作代码)old JSONObject jar
String title = "title";
return new JSONObject()
.put("title", title).put("url", null)));
当我开始使用latest jar from JSON-java时
我收到编译错误
The method put(String, Collection<?>) is ambiguous for the type JSONObject
我发现了一个issue建议使用 JSONObject.NULL
而不是 null
This library has a distinct JSONObject.NULL constant that is used to store JSON null values. It is deliberate. The Java null value is treated in this library like JavaScript undefined.
While not part of the specification, I can't see us undoing this as it would break backwards compatibility.
但是没有中间方法可以同时支持这两个 JSON 版本吗?
是否应该为 JSON-java
项目提供向后支持的错误,或者我是否缺少某些内容或只是使用不受支持的旧版本?
最佳答案
put("", null)
JSONObject 类中的三个方法中的任何一个都可以: put(String key, Object value)
, put(String key, Collection<?> value)
或put(String key, Map<?, ?> value)
。所以最新版本的org.json
不能再使用了图书馆。为了克服这个问题,他们定义了一个静态类 Null
他们自己的:org.json.JSONObject.Null
当用于 put
时会打电话put(String key, Object value)
方法。
关于java - 当输入空值时,JSON-java编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53919711/