- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
在调试一段将字符串反序列化为对象的代码时,我意识到 JSON 解析正在使用对象的 toString 表示而不是 json 表示。
波乔:
import lombok.Builder;
import lombok.Data;
@Data
@Builder
public class Pojo {
int a;
String b;
}
解析逻辑:
Gson gsonInstance =
new GsonBuilder()
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.setDateFormat("yyyy-MM-dd")
.create();
String json2 = "{a=5, b=field}";
Pojo obj = gsonInstance.fromJson(json2, Pojo.class);
System.out.println(obj);
当我向 Pojo 添加包含“:”的字段时,这个问题开始出现问题。就像日期一样。
围绕这个问题有两个问题:1)Gson 是如何支持这个的?我找不到任何医生。一样的。2)如何使用这种 toString 类型的实现来处理我之前提到的日期相关问题?我尝试转义 Java 字符串,但这没有帮助。
最佳答案
这是Gson的一个特性,它实际上有文档记录,但文档有点隐藏。引用:Gson.java 。 JSON反序列化最终调用的是
public <T> T fromJson(JsonReader reader, Type typeOfT) throws JsonIOException, JsonSyntaxException
在代码正文中,请参阅:
reader.setLenient(true);
现在引用JsonReader.java
public final void setLenient(boolean lenient)
该方法的 javadoc 声明如下:
/** * Configure this parser to be liberal in what it accepts. By default, * this parser is strict and only accepts JSON as specified by RFC 4627. Setting the * parser to lenient causes it to ignore the following syntax errors: * *
*
*/- Streams that start with the non-execute * prefix,
")]}'\n"
. *- Streams that include multiple top-level values. With strict parsing, * each stream must contain exactly one top-level value. *
- Top-level values of any type. With strict parsing, the top-level * value must be an object or an array. *
- Numbers may be {@link Double#isNaN() NaNs} or {@link * Double#isInfinite() infinities}. *
- End of line comments starting with {@code //} or {@code #} and * ending with a newline character. *
- C-style comments starting with {@code /*} and ending with * {@code *}{@code /}. Such comments may not be nested. *
- Names that are unquoted or {@code 'single quoted'}. *
- Strings that are unquoted or {@code 'single quoted'}. *
- Array elements separated by {@code ;} instead of {@code ,}. *
- Unnecessary array separators. These are interpreted as if null * was the omitted value. *
- Names and values separated by {@code =} or {@code =>} instead of * {@code :}. *
- Name/value pairs separated by {@code ;} instead of {@code ,}. *
以上就是Gson解析你的字符串并反序列化它的原因。
如果您的名称/值(不带引号)有任何“:”,则上述内容将不起作用,原因是 Gson 会(在您的情况下)将其解释为 name=[value 或 anotherName???]:value (Gson 并不那么宽松)。
使用正确有效的 JSON 并反序列化。
关于java - Gson 解析器接受 toString 表示而不是 Json 语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57554373/
正在阅读 Underscore.js 以了解它的 is[String|Number|...] 方法是如何工作的,现在我很困惑。下划线: toString.call(obj) == ['object '
scala> Array(1, 2, 3).toString res1: String = [I@11cf437c scala> List(1, 2, 3).toString res2: String
我在将字符串从 stringbuilder 转换为字符串时遇到问题。问题类似于 this issue但略有不同: 这是我的简化代码: StringBuilder sb = new StringBuil
我正在尝试将从正在构建的搜索功能中名为 Part 的模型返回的 int id 转换为字符串,以便简化搜索。 这是我目前使用的 if 语句: if(part.getId().toString().ind
我需要从所选内容中提取文本并将其发送到 TTS 服务。 TTS 服务将返回一个流 URL 和每个单词的一组索引,指示它们的开始和结束位置(时间和文本)。 当用户播放流时,我想在读出每个单词时突出显示它
我想知道人们在 Java 的 toString() 方法中放入了什么。 我一直在向一些新类添加一些内容,并且想知道它是否应该包含类名。 在类ClassConfig中,我无法决定是否应该拥有 @Over
这个问题已经有答案了: How do I compare strings in Java? (23 个回答) 已关闭 8 年前。 下面是我的主要方法,其中比较两个对象引用。覆盖toString()方法
我的问题是,JAVA中没有提供toString()方法的类是否可以打印出特定信息? 问题在于:我们为我们的应用程序提供了一个记录器(使用aspectJ),它打印出给出的特定参数。例如: public
基本上这就是我想要实现的目标。 classname@address(?)[original toString()], object's name, object's age @Override pub
据我所知,Scala 中的中缀运算符的使用应该等同于方法的调用。所以: scala> "a" + 3.toString res0: java.lang.String = a3 是相同的: scala>
这个问题已经有答案了: Why can't I access a property of an integer with a single dot? (5 个回答) 已关闭 7 年前。 functio
我正在进行测试,并且给出了很多单元(隐藏)测试,但是我的一段代码遇到了这个错误。大家能帮帮我吗? getString(comment) { const authorName = comment.get
return toString.call(obj) 和 return obj.toString() 有什么区别? 我通常会找到具有这些不同风格的代码 最佳答案 toString.call(obj) 返
例如,我必须在每个数字到字符串的转换中使用 .ToString(CultureInfo.CurrentCulture)。我能否以某种方式重写 .ToString(),这样我就不会在字符串转换中显式地收
var d = []; console.log(typeof d); // weird! console.log(d.toString()); //Prints nothing since there
当对象字面量调用toString()方法如{}.toString()会导致语法错误,但是当数组字面量调用toString()没关系。当我将对象文字分配给一个变量时,当它调用 toString() 方法
我在打印特殊数组时遇到问题: 我使用 System.out.println(Arrays.toString()); 打印多个对象的数组但现在数组中充满了对象,这些对象具有 char 值,我想打印分配给
关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。 这个问题是由于错别字或无法再重现的问题引起的。虽然类似的问题可能是on-topi
> ~0..toString(2) -1 > ~1..toString(2) -2 > ~2..toString(2) -11 > ~3..toString(2) -12 > (~1).toStrin
这是我的问题,我的机器使用法语文化,因此默认情况下它以法语方式解析 (3,141592)。 如果机器文化不是美国,这里是重现我的问题的代码: float number = 4103.26808
我是一名优秀的程序员,十分优秀!