gpt4 book ai didi

java - 当输入空值时,JSON-java编译错误

转载 作者:行者123 更新时间:2023-12-02 10:26:31 26 4
gpt4 key购买 nike

我有以下(工作代码)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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com