JSON.NET Parser *seems* to be double serializing my objects(JSON.NET Parser *似乎*对我的对象进行双重序列化)
问题描述
我的问题是这样的:
这是从我的 WebAPI 控制器发回的响应.
This is the response being sent back from my WebAPI controller.
"[
[
{"id":"identifier"},
{"name":"foobar"}
]
]"
请注意,响应包含在引号中,并且所有嵌入的引号都被转义了.这显然是个问题.我可以向 JSON.NET 序列化程序提供任何设置来防止这种情况发生吗?
Notice that the response is wrapped in quotations and all of the embedded quotations are escaped. This is obviously a problem. Are there any settings I can provide to the JSON.NET Serializer to prevent this from occurring?
正如 p.s.w.g 在他的回复中猜测的那样,我使用的是 JSON.NET 的
As p.s.w.g guessed in his response, I was using JSON.NET's
JsonConvert.SerializeObject(instance)
执行我的序列化.
我这样做是因为在构建自定义转换器时,我已将它们包含在我的 WepApiConfig 中的 JsonConvert.DefaultSettings 中(我显然认为这不会成为问题)
I did this because as I was building out my custom Converters, I had included them in the JsonConvert.DefaultSettings within my WepApiConfig (and I obviously thought this would not be a problem)
我之前曾尝试将我的 HttpGets 的返回类型交换为我的对象类型",并且响应是我的对象的 ToString() 方法的 json 表示...这让我知道序列化没有通过我的转换器.
I had previously tried to swap the return type of my HttpGets to "my object type" and the response was a json representation of my object's ToString() method...which let me know that serialization was not passing through my converters.
将我的 HttpGets 的返回类型从字符串更改为我的对象类型"并将这些转换器直接插入 WebAPi 的默认 HttpConfiguration 就可以了.
Changing the return type of my HttpGets from string to "my object type" and plugging those converters straight into WebAPi's default HttpConfiguration did the trick.
config.Formatters.JsonFormatter.SerializerSettings.Converters.Add(new FooConverter());
config.Formatters.JsonFormatter.SerializerSettings.Converters.Add(new BarConverter());
简单易懂.
推荐答案
你可能有这样的情况:
public string GetFoobars()
{
var foobars = ...
return JsonConvert.SerializeObject(foobars);
}
在这种情况下,您将使用 Json.NET 将对象序列化为字符串,然后通过将结果作为字符串返回,API 控制器会将字符串序列化为 JavaScript 字符串文字——这将导致字符串被包装在双引号中并导致字符串中的任何其他特殊字符用反斜杠转义.
In this case, you're serializing the object into string with Json.NET, then by returning the result as a string, the API controller will serialize the string as a JavaScript string literal—which will cause the string to be wrapped in double quotes and cause any other special characters inside the string to escaped with a backslash.
解决方案是简单地自己返回对象:
The solution is to simply return the objects by themselves:
public IEnumerable<Foobar> GetFoobars()
{
var foobars = ...
return foobars;
}
这将导致 API 控制器使用其默认设置序列化对象,这意味着它将根据从客户端传入的参数将结果序列化为 XML 或 JSON.
This will cause the API controller to serialize the objects using it's default settings, meaning it will serialize the result as XML or JSON depending on the parameters passed in from the client.
进一步阅读
- ASP.NET Web 中的 JSON 和 XML 序列化API
这篇关于JSON.NET Parser *似乎*对我的对象进行双重序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:JSON.NET Parser *似乎*对我的对象进行双重序列化
基础教程推荐
- c# Math.Sqrt 实现 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- SSE 浮点算术是否可重现? 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01