简体   繁体   中英

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

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.

Then I should get a query object which has 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#. Most of deserializing sdk use the reflection, and in this case is impossible. I tested your sample without the dash on "query-continue" property and it's work fine.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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