json.net在序列化和反序列化一个非常简单的类时抛出了一个错误(json.net is throwing an error on serializing and deserializing a v

编程入门 行业动态 更新时间:2024-10-22 08:30:54
json.net在序列化和反序列化一个非常简单的类时抛出了一个错误(json.net is throwing an error on serializing and deserializing a very simple class)

我正在与Json合作,并将其从一个应用程序返回到另一个应用程序,这是我第一次认真尝试。

我的应用程序是使用.Net 4.0 Framework的ASP.NET MVC 3应用程序。

我需要序列化和反序列化一个非常简单的类到json和从json。

public class ProxyRequestResultDetails { public string ApplicationName { get; set; } public string ProxyValue { get; set; } public bool ProxyRelationshipExists { get; set; } }

该类不会继承或执行任何操作。

我可以通过执行以下操作将它成功转换为json:

string json = JsonConvert.SerializeObject(requestDetails);

创建的json的一个例子是:

"{\"ApplicationName\":\"Awesome App\",\"ProxyValue\":\"0\",\"ProxyRelationshipExists\":true}"

然后我打电话给:

ProxyRequestResultDetails deserializedTestRequestDetails = JsonConvert.DeserializeObject<ProxyRequestResultDetails>(json);

这有一个像下面的堆栈跟踪。 它已被格式化为包含所有内部例外的赌注。

Method: CustomHandleErrorAttribute Message: Error converting value "{\"ApplicationName\":\"Awesome App\",\"ProxyValue\":\"0\",\"ProxyRelationshipExists\":true}" to type 'ProxySetup.Models.ProxyRequestResultDetails'. Path '', line 1, position 98. Inner Error #1: Could not cast or convert from System.String to ProxySetup.Models.ProxyRequestResultDetails. stack trace: at Newtonsoft.Json.Utilities.ConvertUtils.EnsureTypeAssignable(Object value, Type initialType, Type targetType) at Newtonsoft.Json.Utilities.ConvertUtils.ConvertOrCast(Object initialValue, CultureInfo culture, Type targetType) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureType(JsonReader reader, Object value, CultureInfo culture, JsonContract contract, Type targetType)

这似乎是一个非常简单的对象。 我可能错过了一些简单的东西,但是我无法从示例中找到具体的东西。 任何想法将不胜感激。

编辑1

严肃是对的。 这正是发生了什么,但我不知道如何适应它。 我想要做的是在控制器上公开一个动作,以便它可以像Web服务调用一样使用。 在操作中作为原始字符串创建的json是:

{"ApplicationName":"Awesome App","ProxyValue":"0","ProxyRelationshipExists":true}

但由动作返回的json(带有返回类型的JsonResult)

return Json(json, "application/json; charset=utf-8", JsonRequestBehavior.AllowGet);

是:

"{\"ApplicationName\":\"Awesome App\",\"ProxyValue\":\"0\",\"ProxyRelationshipExists\":true}"

思考?

编辑2 - 已解决

这很简单。 我将操作返回类型更改为

string

并从以下位置返回结果字符串:

string json = JsonConvert.SerializeObject(requestDetails);

呼叫。

非常感谢!

I'm taking my first serious foray into working with Json and returning it from one application to another.

My application is an ASP.NET MVC 3 application using the .Net 4.0 Framework.

I need to serialize and deserialize a very simple class to and from json.

public class ProxyRequestResultDetails { public string ApplicationName { get; set; } public string ProxyValue { get; set; } public bool ProxyRelationshipExists { get; set; } }

The class doesn't inherit or implement anything.

I can successfully convert it to json by doing:

string json = JsonConvert.SerializeObject(requestDetails);

An example of the json created is:

"{\"ApplicationName\":\"Awesome App\",\"ProxyValue\":\"0\",\"ProxyRelationshipExists\":true}"

Then I make the following call:

ProxyRequestResultDetails deserializedTestRequestDetails = JsonConvert.DeserializeObject<ProxyRequestResultDetails>(json);

This has a stack trace like the one below. It's been formatted a bet to include all inner exceptions.

Method: CustomHandleErrorAttribute Message: Error converting value "{\"ApplicationName\":\"Awesome App\",\"ProxyValue\":\"0\",\"ProxyRelationshipExists\":true}" to type 'ProxySetup.Models.ProxyRequestResultDetails'. Path '', line 1, position 98. Inner Error #1: Could not cast or convert from System.String to ProxySetup.Models.ProxyRequestResultDetails. stack trace: at Newtonsoft.Json.Utilities.ConvertUtils.EnsureTypeAssignable(Object value, Type initialType, Type targetType) at Newtonsoft.Json.Utilities.ConvertUtils.ConvertOrCast(Object initialValue, CultureInfo culture, Type targetType) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureType(JsonReader reader, Object value, CultureInfo culture, JsonContract contract, Type targetType)

This seems to be a very simple object. I'm probably missing something simple, but I can't find what exactly from the examples. Any thoughts would be appreciated.

EDIT 1

Serious is right. That's exactly what's happening, but I'm not sure how to fit it. What I'm trying to do is expose an action on the controller such that it could be used like a web service call. The json created in the action as a raw string is:

{"ApplicationName":"Awesome App","ProxyValue":"0","ProxyRelationshipExists":true}

but the json returned by the action (with a return type of JsonResult)

return Json(json, "application/json; charset=utf-8", JsonRequestBehavior.AllowGet);

is:

"{\"ApplicationName\":\"Awesome App\",\"ProxyValue\":\"0\",\"ProxyRelationshipExists\":true}"

Thoughts?

EDIT 2 - SOLVED

This was simple enough. I changed the actions return type to

string

and returned the resulting string from the:

string json = JsonConvert.SerializeObject(requestDetails);

call.

Thanks Serious!

最满意答案

看起来你的序列化字符串已经被转义或者类似的东西,使得它表现为表示字符串的字符串,而不是表示JSON流的字符串。

举个例子 :

string json = "{}"; string notJson = "\"{}\"";

例如,使用VS调试器检查字符串的原始值。

It seems like your serialized string has been escaped or something like that, making it appears as a string representing a string, not a string representing a JSON stream.

As an example :

string json = "{}"; string notJson = "\"{}\"";

So check the raw value of your string using the VS debugger for example.

更多推荐

本文发布于:2023-07-20 02:58:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1192115.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:序列化   抛出   错误   简单   net

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!