反序列化 JSON 子文档 [英] Deserialize JSON subdocument
本文介绍了反序列化 JSON 子文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在调用 JIRA Rest API 以接收工作日志对象列表.
I'm calling the JIRA Rest API to recieve a list of Worklog Objects.
我收到的 JSON 看起来像.
The JSON I recieve looks like.
{
"startAt": 0,
"maxResults": 1,
"total": 1,
"worklogs": [
{
"self": "http://www.example.com/jira/rest/api/2/issue/10010/worklog/10000",
"author": {
"self": "http://www.example.com/jira/rest/api/2/user?username=fred",
"name": "fred",
"displayName": "Fred F. User",
"active": false
},
"updateAuthor": {
"self": "http://www.example.com/jira/rest/api/2/user?username=fred",
"name": "fred",
"displayName": "Fred F. User",
"active": false
},
"comment": "I did some work here.",
"visibility": {
"type": "group",
"value": "jira-developers"
},
"started": "2015-08-25T07:43:10.086+0000",
"timeSpent": "3h 20m",
"timeSpentSeconds": 12000,
"id": "100028"
}
]
}
正如我所说,我想把它放在一个列表中.
As I said, I want to put it in a list.
var json = client.MakeRequest("", password, user);
List<Worklog> myList = JsonConvert.DeserializeObject<List<Worklog>>(json);
它不起作用,因为
"startAt": 0,
"maxResults": 1,
"total": 1,
如何让解串器忽略这些属性?感谢您的帮助!
How can I make the deserializer ignore those properties? Thanks for your help!
推荐答案
要么创建一个包含属性的RootObject"类:
Either create a "RootObject" class that does contain the properties:
public class RootObject
{
public int startAt { get; set; }
public int maxResults { get; set; }
public int total { get; set; }
public List<Worklog> worklogs { get; set; }
}
并反序列化为:
var rootObject = JsonConvert.DeserializeObject<RootObject>(json);
// access rootObject.worklogs
或者进入解析后的 JSON 并从那里反序列化:
Or step into the parsed JSON and deserialize from there:
JObject o = JObject.Parse(json);
JToken worklogsJson = o.SelectToken("worklogs");
var worklogs = worklogsJson.ToObject<List<Worklog>>();
这篇关于反序列化 JSON 子文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文