获取“拖延字符”尝试用SERDE_JSON分析字符串时的错误
我需要在serde_json :: value中转换一个特定的字符串,但是在使用时会遇到尾随字符错误:
let new_str = "73723235c81ebbec0"
let json_value: serde_json::Value = serde_json::from_str(new_str).unwrap();
有
trailing characters at line 1 column 9
没有办法将这些字符串转换为serde_json :: value?
I need to convert a specific string in serde_json::Value, but getting a trailing character error while using:
let new_str = "73723235c81ebbec0"
let json_value: serde_json::Value = serde_json::from_str(new_str).unwrap();
Error:
trailing characters at line 1 column 9
Is there any way to convert these kinds of strings to serde_json::Value?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
查看
serde_json
的存储库,从他们的测试用例中出现这是预期的行为。例如,请参见此而不是调用
from_str()
,请使用to_value( )
。这将为您提供String
value
枚举的变体。另外,如果您使用
to_value()
,则可以省略变量的类型标识符,我相信这可能更惯用。Looking at the repo for
serde_json
, it appears from their test cases this is expected behavior. For example, see this test case. Since your string starts with numeric values, it is attempting to parse as a number and then fails when it reaches the 'c' in position 9.Rather than calling
from_str()
, useto_value()
. This will give you aString
variant of theValue
enum.Also, if you use
to_value()
, you can omit the type identifier for your variable, which I believe is probably more idiomatic.