gpt4 book ai didi

c# - 为什么我的 JSON 中没有添加大括号?

转载 作者:太空宇宙 更新时间:2023-11-03 19:45:29 24 4
gpt4 key购买 nike

为什么下面的代码没有在 json 的值周围输出大括号?我以为 JSON 总是封装在括号内。

var json = JsonConvert.SerializeObject("some text into json", Formatting.Indented);`

生成的 JSON 现在是 "\"some text into json\"" 而不是 { "\"some text into json\""}

最佳答案

这是因为您只是序列化了一个字符串。因此,序列化器只返回序列化的字符串,在 JSON 中,它只是用引号引起来的字符串:

"some text"

JSON 仅在序列化对象时添加大括号:

{
"someStringProperty": "some text"
}

另请注意,输出中的反斜杠是 Visual Studio 再次将整个字符串封装在引号中,并且还会对字符串中的引号进行转义。序列化字符串的“真实”值只使用简单的引号。因此 Visual Studio 将显示上述 JSON 字符串,如下所示:

"\"some text\""

"{ \"someStringProperty\": \"some text\" }"

关于c# - 为什么我的 JSON 中没有添加大括号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46338372/

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