简体   繁体   English

我应该如何创建json类以使用C#进行解析?

[英]How should I create my json classes to parse this with c#?

I've used several json to C# converters to generate classes so I can deserialize it with 我使用了几个json到C#转换器来生成类,因此可以使用

var foo = JsonConvert.DeserializeObject<someGeneratedType>(jsonString);

However I can't seem to get the results populated. 但是我似乎无法填充结果。 A query-continue object should have usercontribs which has ucstart which has a value. 查询继续对象应具有ucstart值的usercontribs

Then I should get a query object which has 3 usercontribs . 然后我应该得到一个具有3个usercontribs查询对象。 I'd be much obliged if someone could help me figure this out. 如果有人可以帮助我解决这个问题,我将非常有责任。

{
  "query-continue": {
    "usercontribs": {
      "ucstart": "2013-07-18T02:24:25Z"
    }
  },
  "query": {
    "usercontribs": [
      {
        "userid": "666777",
        "user": "UserYahoo",
        "pageid": 22255,
        "revid": 555566666,
        "parentid": 555577777,
        "ns": 0,
        "title": "Title A",
        "timestamp": "2013-07-16T01:13:32Z",
        "comment": "/* Comment A */",
        "size": 62789
      },
      {
        "userid": "666777",
        "user": "UserYahoo",
        "pageid": 22255,
        "revid": 564444444,
        "parentid": 555566666,
        "ns": 0,
        "title": "Title A",
        "timestamp": "2013-07-16T01:28:50Z",
        "comment": "/* Comment B */",
        "size": 62794
      },
      {
        "userid": "666777",
        "user": "UserYahoo",
        "pageid": 11777,
        "revid": 564333333,
        "parentid": 444499999,
        "ns": 0,
        "title": "Title B",
        "timestamp": "2013-07-17T03:28:50Z",
        "comment": "/* Comment C */",
        "size": 10865
      }
    ]
  }
}

The root element "query-continue" doesn't respond on named rules properties in C#. 根元素“ query-continue”对C#中的命名规则属性不响应。 Most of deserializing sdk use the reflection, and in this case is impossible. 大部分反序列化sdk使用反射,在这种情况下是不可能的。 I tested your sample without the dash on "query-continue" property and it's work fine. 我测试了您的样本,但在“ query-continue”属性上没有破折号,并且工作正常。

public class Usercontribs
    {
        public string ucstart { get; set; }
    }

    public class Querycontinue
    {
        public Usercontribs usercontribs { get; set; }
    }

    public class Usercontrib
    {
        public string userid { get; set; }
        public string user { get; set; }
        public int pageid { get; set; }
        public int revid { get; set; }
        public int parentid { get; set; }
        public int ns { get; set; }
        public string title { get; set; }
        public string timestamp { get; set; }
        public string comment { get; set; }
        public int size { get; set; }
    }

    public class Query
    {
        public List<Usercontrib> usercontribs { get; set; }
    }

    public class RootObject
    {
        public Querycontinue querycontinue { get; set; }
        public Query query { get; set; }
    }

And i test it : 我测试一下:

Stream fs = File.OpenRead(@"C:\Users\Dell\Downloads\sample.txt");

DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(RootObject));
RootObject v_response = (RootObject)jsonSerializer.ReadObject(fs);

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

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