我应该如何创建我的 json 类来使用 c# 解析它



我使用了几个 json 到 C# 转换器来生成类,所以我可以用

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

但是,我似乎无法填充结果。 一个查询继续对象应该有usercontribs,它有一个值的ucstart

然后我应该得到一个有 3 个用户贡献查询对象。 如果有人能帮我解决这个问题,我会非常有义务。

{
  "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
      }
    ]
  }
}

元素"query-continue"不响应 C# 中的命名规则属性。大多数反序列化 sdk 都使用反射,在这种情况下是不可能的。我在"查询-继续"属性上没有破折号的情况下测试了您的样本,它工作正常。

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

我测试它:

Stream fs = File.OpenRead(@"C:UsersDellDownloadssample.txt");
DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(RootObject));
RootObject v_response = (RootObject)jsonSerializer.ReadObject(fs);

相关内容

  • 没有找到相关文章

最新更新