在C#中,如何为具有多个嵌套数组的JSON对象建模? [英] In C#, how do I model a JSON object with multiple nested arrays?

查看:156
本文介绍了在C#中,如何为具有多个嵌套数组的JSON对象建模?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从连接到的系统中获取此JSON响应,并试图找出将其反序列化为C#对象的最佳方法.我目前正在使用 RestSharp ,这似乎很容易使用,但是JSON的格式让我有些困惑.这是它的格式:

I am getting this JSON response from a system I am connecting to and trying to figure out the best way to deserialize it into a C# object. I am currently using RestSharp which seems pretty straight forward to use but the format of the JSON is baffling me a bit. Here is the format that its coming in as:

[
  {"name": "Tickets:",
   "schema": [
    {"dataType": "string", "colName": "First", "idx": 0}, 
    {"dataType": "string", "colName": "Second", "idx": 1}, 
    {"dataType": "string", "colName": "Name", "idx": 2}
   ], 
   "data": [
            ["bill", "test", "joe"],
            ["bill2", "test2", "joe2"],
            ["bill3", "test3", "joe3"]
           ]
  }
] 

这是我当前的代码:

var url = "http://myUrl:10111";
var client = new RestClient { BaseUrl = url };

var request = new RestRequest { Method = Method.GET, Resource = "/search?fmt=Json", RequestFormat = DataFormat.Json };
request.AddHeader("accept", "application/json");

var response = client.Execute(request);
var wptResponse = new JsonDeserializer().Deserialize<TicketResults>(response);
return wptResponse;

但是如上所述,我正在尝试找出对TicketResults对象建模的正确方法,以支持在上面反序列化此消息.

but as stated above I am trying to figure out the correct way to model the TicketResults object to support deserializing this message above.

理想情况下,我想要这样的东西:

Ideally I would like something like this:

 public class TicketResults
 {
     public List<Ticket> Tickets {get;set;}
 }

 public class Ticket
 {
     public string First {get;set;}
     public string Second {get;set;}
     public string Name {get;set;}
 }

,在上面的示例中,Tickets集合中将获得三个条目.

and in this example above would get three entries in the Tickets collection.

此外,上述JSON格式是否正常,因为我从未见过将其分解为单独的模式和数据部分(我可以看到它可以节省一些空间,但是在这种情况下,消息并不那么大)

Also, is the above JSON format normal as i have never seen this broken out into separate schema and data section (I can see where it might save some space but in this case the messages are not that big)

推荐答案

我同意json格式相当……愚蠢.这是对dto建模的方法:

I agree the json format is quite ... goofy. Here's how to model your dto:

    public class JsonDto
    {
        public string name { get; set; }
        public Schema[] schema {get; set;}
        public string[][] data { get; set; }
    }
    public class Schema
    {
        public string dataType { get; set; }
        public string colName { get; set; }
        public int idx { get; set; }
    }

我能够使用 JSON.Net 将您的字符串(未更改)反序列化,如下所示:

I was able to get your string (unaltered) to deserialize with JSON.Net like this:

var jsonDto = JsonConvert.DeserializeObject<JsonDto[]>(json);

让我知道您是否仍然遇到麻烦.

Let me know if you're still having trouble.

这篇关于在C#中,如何为具有多个嵌套数组的JSON对象建模?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆