[英]Unable to Serialize/Deserialize List<> object into JSON
I am working on 2 web applications; 我正在开发2个Web应用程序; A & B. now i have a shared class named CRUDOutput as follow on both web applications:-
A&B。现在我在两个Web应用程序中都有一个名为CRUDOutput的共享类: -
public class CRUDOutput
{
public Operation4 operation { get; set; }
}
public class Operation4
{
public Result result { get; set; }
public string name { get; set; }
}
public class Result
{
public string status { get; set; }
public string message { get; set; }
}
now inside web application A i am returning the following:- 现在在Web应用程序中我正在返回以下内容: -
[HttpPost]
public ActionResult CreateResource(CreateResource cr)
{
List<CRUDOutput> co = new List<CRUDOutput>();
co.Add(JsonConvert.DeserializeObject<CRUDOutput>(crudoutput));
co.Add(JsonConvert.DeserializeObject<CRUDOutput>(crudoutput2));
return Json(JsonConvert.SerializeObject(co));
}
now from web application B, i am calling the action method as follow:- 现在从Web应用程序B,我调用的操作方法如下: -
try
{
using (WebClient wc = new WebClient())
{
string url = "https://localhost:44302/" + "Home/CreateResource";
Uri uri = new Uri(url);
wc.Headers.Add(HttpRequestHeader.ContentType, "application/json");
output = wc.UploadString(uri, data);
}
}
catch (WebException e)
{
}
List<CRUDOutput> result = JsonConvert.DeserializeObject<List< CRUDOutput>>(output);
but i will get the following exception when i tried to deserialize the output:- 但是当我试图反序列化输出时,我会得到以下异常: -
Error converting value "[{"operation":{"result":{"status":"Success","message":"Resource has been added successfully to ......"},"name":"CREATE RESOURCE"}},{"operation":{"result":{"status":"Failed","message":"Account addition "},"name":"ADD ACCOUNTS"}}]" to type 'System.Collections.Generic.List`1[S.ViewModels.CRUDOutput]'.
转换值时出错“[{”operation“:{”result“:{”status“:”Success“,”message“:”资源已成功添加到......“},”name“:”CREATE资源“}},{”操作“:{”结果“:{”状态“:”失败“,”消息“:”帐户添加“},”名称“:”添加帐户“}}]”输入'系统.Collections.Generic.List`1 [S.ViewModels.CRUDOutput]”。 Path '', line 1, position 464.
路径'',第1行,第464位。
now the JSON return from web application A will be as follow:- 现在,从Web应用程序A返回的JSON将如下: -
"\"[{\\\"operation\\\":{\\\"result\\\":{\\\"status\\\":\\\"Success\\\",\\\"message\\\":\\\"Resource 123 rfrf has been added successfully \\\"},\\\"name\\\":\\\"CREATE RESOURCE\\\"}},{\\\"operation\\\":{\\\"result\\\":{\\\"status\\\":\\\"Failed\\\",\\\"message\\\":\\\"Account addition \\\"},\\\"name\\\":\\\"ADD ACCOUNTS\\\"}}]\""
so can anyone advice why i am unable to deserialize to a list of objects? 所以任何人都可以建议为什么我无法反序列化为对象列表?
The output as you've pasted is encoded as JSON twice. 您粘贴的输出被编码为JSON两次。 Compare the difference between:
比较两者之间的区别:
"\"[{\\\"operation\\\":{\\\"result\\\":{\\\"status\\\":\\\"Success\\\",\\\"message\\\":\\\"Resource 123 rfrf has been added successfully \\\"},\\\"name\\\":\\\"CREATE RESOURCE\\\"}},{\\\"operation\\\":{\\\"result\\\":{\\\"status\\\":\\\"Failed\\\",\\\"message\\\":\\\"Account addition \\\"},\\\"name\\\":\\\"ADD ACCOUNTS\\\"}}]\""
and 和
"[{\"operation\":{\"result\":{\"status\":\"Success\",\"message\":\"Resource 123 rfrf has been added successfully \"},\"name\":\"CREATE RESOURCE\"}},{\"operation\":{\"result\":{\"status\":\"Failed\",\"message\":\"Account addition \"},\"name\":\"ADD ACCOUNTS\"}}]"
This happens because you're encoding the result as Json twice. 发生这种情况是因为您将结果编码为Json两次。 Replace:
更换:
return Json(JsonConvert.SerializeObject(result));
with 同
return Json(result); // This encodes as JSON automatically
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.