简体   繁体   English

在C#中,我如何反序列化此json?

[英]In C#, how can i deserialize this json?

I am getting this JSON data back from a REST service that looks like this: 我从看起来像这样的REST服务获取此JSON数据:

[ [ [ {"Name": "Joe", "Comment": "Load", "Updated": null, "Test": ""} ] ] ]

or 要么

[ [ [ {"Name": "Joe", "Comment": "Load", "Updated": null, "Test": ""}, {"Name": "Bill", "Comment": "123", "Updated": null, "Test": ""} ] ] ]

I did a "Copy JSON as classes" feature in visual studio and it created this: 我在Visual Studio中做了一个“将JSON复制为类”功能,它是这样创建的:

 public class Rootobject
 {
     public Project[][][] Property1 { get; set; }
 }

 public class Project
 {
   public string Name { get; set; }
   public string Comment { get; set; }
   public string Updated { get; set; }
   public string Test { get; set; }
 }

but when i try to deserialize using this code: 但是当我尝试使用此代码反序列化时:

 var results = new JsonDeserializer().Deserialize<Rootobject>(response);

I get an error stating: 我收到一条错误消息:

An exception of type 'System.InvalidCastException' occurred in RestSharp.dll but was not handled in user code Additional information: Unable to cast object of type 'RestSharp.JsonArray' to type 'System.Collections.Generic.IDictionary`2[System.String,System.Object]' RestSharp.dll中发生类型'System.InvalidCastException'的异常,但未在用户代码中处理。其他信息:无法将类型为'RestSharp.JsonArray'的对象转换为类型为'System.Collections.Generic.IDictionary`2 [System.System。 String,System.Object]'

Could you please advise on what I might be doing wrong (NOTE: i don't have any control over how the data comes in so changing the format isn't an option) 您能否就我可能做错的事情提出建议(注意:我对数据的输入方式没有任何控制权,因此不能更改格式)

Also, to confirm this seems to be valid JSON from JSONLlint: 另外,要确认这似乎是来自JSONLlint的有效JSON,请执行以下操作:

在此处输入图片说明

Using Newtonsoft.Json nuget package, try 使用Newtonsoft.Json nuget包,尝试

JSONConvert.DeserialiseObject<Rootobject>(response)

EDIT: BTW, I tried to use your json on http://json2csharp.com/ and it says Parsing your JSON didn't work. 编辑:顺便说一句,我试图在http://json2csharp.com/上使用您的json,它说解析您的JSON不起作用。 Please make sure it's valid. 请确保它是有效的。 So I doubt that any json parsing library will be able to parse your JSON. 因此,我怀疑任何json解析库都将能够解析您的JSON。

However implementing your own deserializer is possible and an ideal solution when external services return invalid JSON 但是,可以实现自己的反序列化器,并且是在外部服务返回无效JSON时的理想解决方案

I can probably help you deserialize it if you show me what JSON you get when service returns multiple Project objects. 如果您向我展示了当服务返回多个Project对象时得到的JSON,我可能会帮助您反序列化。

EDIT2: Szabolcs's solutions seems promising, but I would still suggest testing it with JSON for multiple Product objects. EDIT2:Szabolcs的解决方案似乎很有希望,但我仍然建议使用JSON对多个Product对象进行测试。 I smell something bad & its the shitty third party service. 我闻到了不好的东西,还有糟糕的第三方服务。 Always good to test. 总是很好的测试。

You can deserialize that particular JSON like this using Json.NET: 您可以使用Json.NET这样反序列化特定的JSON:

var json = "[ [ [ {\"Name\": \"Joe\", \"Comment\": \"Load\", \"Updated\": null, \"Test\": \"\"}, "+ 
                " {\"Name\": \"Bill\", \"Comment\": \"123\", \"Updated\": null, \"Test\": \"\"} ] ] ]";

var deserializedObject = JsonConvert.DeserializeObject<List<List<List<Project>>>>(json);

And to get all the Project s from the nested lists: 并从嵌套列表中获取所有Project

var allProjects = deserializedObject.SelectMany(x => x.SelectMany(y => y)).ToList();

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM