简体   繁体   English

无法将List <>对象序列化/反序列化为JSON

[英]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

protobuf 和列表<object> - 如何序列化/反序列化?<div id="text_translate"><p> 我有一个List&lt;object&gt; ,其中包含不同类型的对象,例如整数、字符串和自定义类型。 所有自定义类型都经过 protobuf 调整。 我现在想做的是用 protobuf.net 序列化/反序列化这个列表。 到目前为止,我怀疑我必须显式声明每种类型,不幸的是,这些混合列表构造不可能做到这一点。 因为二进制格式化程序在做这些事情时没有问题,我希望我错过了一些东西,你可以帮助我。 所以我的问题是如何处理 protobuf.net 中的对象。</p></div></object> - protobuf and List<object> - how to serialize / deserialize?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 序列化/反序列化列表<t>至 JSON</t> - serialize/deserialize List<T> to JSON json.net无法反序列化到对象列表 - json.net Unable to Deserialize to Object List 无法反序列化JSON对象 - Unable to deserialize JSON object 反序列化对象列表 - Deserialize Object List Json 将JSON反序列化为对象列表 - Deserialize json to list of object JSON 用列表进行序列化和反序列化<string>不工作</string> - JSON Serialize and Deserialize with List<string> not working 如何正确序列化和反序列化 Json 中的对象列表? - How correctly serialize and deserialize List of objects in Json? 序列化一个对象并反序列化为对象列表 - Serialize one object and deSerialize into a list of objects 序列化和反序列化对象列表,不破坏封装 - Serialize and Deserialize list of object, without break encapsulation protobuf 和列表<object> - 如何序列化/反序列化?<div id="text_translate"><p> 我有一个List&lt;object&gt; ,其中包含不同类型的对象,例如整数、字符串和自定义类型。 所有自定义类型都经过 protobuf 调整。 我现在想做的是用 protobuf.net 序列化/反序列化这个列表。 到目前为止,我怀疑我必须显式声明每种类型,不幸的是,这些混合列表构造不可能做到这一点。 因为二进制格式化程序在做这些事情时没有问题,我希望我错过了一些东西,你可以帮助我。 所以我的问题是如何处理 protobuf.net 中的对象。</p></div></object> - protobuf and List<object> - how to serialize / deserialize?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM