[英]How should I create my json classes to parse this with c#?
我使用了幾個json到C#轉換器來生成類,因此可以使用
var foo = JsonConvert.DeserializeObject<someGeneratedType>(jsonString);
但是我似乎無法填充結果。 查詢繼續對象應具有ucstart值的usercontribs 。
然后我應該得到一個具有3個usercontribs的查詢對象。 如果有人可以幫助我解決這個問題,我將非常有責任。
{
"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使用反射,在這種情況下是不可能的。 我測試了您的樣本,但在“ 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; }
}
我測試一下:
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.