问题描述
由法我已经JSON字符串返回:
公开名单<问题> GETDATA(
串JQL,
名单<字符串>田= NULL,
INT startAt = 0,
INT maxResult = 500)
{
字符串数据= JsonConvert.SerializeObject(要求);
字符串结果= runQuery(JiraResource.search,数据,POST); //返回结果和工作正常
SearchResponse响应= JsonConvert.DeserializeObject< SearchResponse>(结果);
返回response.IssueDescriptions = GetIssues(响应);;
}
和JSON字符串是这样的:
<$p$p><$c$c>{"expand":"schema,names","startAt":0,"maxResults":50,"total":139,"issues":[{"expand":"operations,versionedRe$p$psentations,editmeta,changelog,transitions,renderedFields","id":"20930","self":"https://clientname.atlassian.net/rest/api/2/issue/20930","key":"-139","fields":{"summary":"Rebate","issuetype":{"self":"https://clientname.atlassian.net/rest/api/2/issuetype/1","id":"1","description":"A费谈判或quote","iconUrl":"https://clientname.atlassian.net/images/icons/issuetypes/sales.png","name":"Fees","subtask":false},"components":[],"created":"2015-07-10T12:29:05.000+1000","description":"Execute按照所附instruction","project":{"self":"https://clientname.atlassian.net/rest/api/2/project/10013","id":"10013","key":"--","name":"Change Instructions","avatarUrls":{"48x48":"https://clientname.atlassian.net/secure/projectavatar?avatarId=10011","24x24":"https://clientname.atlassian.net/secure/projectavatar?size=small&avatarId=10011","16x16":"https://clientname.atlassian.net/secure/projectavatar?size=xsmall&avatarId=10011","32x32":"https://clientname.atlassian.net/secure/projectavatar?size=medium&avatarId=10011"}},"reporter":{"self":"https://clientname.atlassian.net/rest/api/2/user?username=rajap","name":"rajap","key":"rajap","emailAddress":"email.au","avatarUrls":{"48x48":"https://clientname.atlassian.net/secure/useravatar?avatarId=10142","24x24":"https://clientname.atlassian.net/secure/useravatar?size=small&avatarId=10142","16x16":"https://clientname.atlassian.net/secure/useravatar?size=xsmall&avatarId=10142","32x32":"https://clientname.atlassian.net/secure/useravatar?size=medium&avatarId=10142"},"displayName":"Prabu","active":true,"timeZone":"US"},"priority":{"self":"https://clientname.atlassian.net/rest/api/2/priority/3","iconUrl":"https://clientname.atlassian.net/images/icons/priorities/major.png","name":"Medium","id":"3"},"resolution":null,"duedate":"2015-07-15","comment":{"startAt":0,"maxResults":1,"total":1,"comments":[{"self":"https://clientname.atlassian.net/rest/api/2/issue/20930/comment/24290","id":"24290","author":{"self":"https://clientname.atlassian.net/rest/api/2/user?username=acahill","name":"acahill","key":"acahill","emailAddress":"email","avatarUrls":{"48x48":"https://clientname.atlassian.net/secure/useravatar?avatarId=10142","24x24":"https://clientname.atlassian.net/secure/useravatar?size=small&avatarId=10142","16x16":"https://clientname.atlassian.net/secure/useravatar?size=xsmall&avatarId=10142","32x32":"https://clientname.atlassian.net/secure/useravatar?size=medium&avatarId=10142"},"displayName":"Andrew卡希尔,主动:真, - 时区:美国},身:约翰,请action","updateAuthor":{"self":"https://clientname.atlassian.net/rest/api/2/user?username=acahill","name":"acahill","key":"acahill","emailAddress":"email","avatarUrls":{"48x48":"https://clientname.atlassian.net/secure/useravatar?avatarId=10142","24x24":"https://clientname.atlassian.net/secure/useravatar?size=small&avatarId=10142","16x16":"https://clientname.atlassian.net/secure/useravatar?size=xsmall&avatarId=10142","32x32":"https://clientname.atlassian.net/secure/useravatar?size=medium&avatarId=10142"},"displayName":"Andrew","active":true,"timeZone":"US"},"created":"2015-07-10T12:35:43.728+1000","updated":"2015-07-10T12:35:43.728+1000"}]},"votes":{"self":"https://clientname.atlassian.net/rest/api/2/issue/-139/votes","votes":0,"hasVoted":false},"assignee":{"self":"https://clientname.atlassian.net/rest/api/2/user?username=olearyj","name":"olearyj","key":"olearyj","emailAddress":"email","avatarUrls":{"48x48":"https://clientname.atlassian.net/secure/useravatar?avatarId=10142","24x24":"https://clientname.atlassian.net/secure/useravatar?size=small&avatarId=10142","16x16":"https://clientname.atlassian.net/secure/useravatar?size=xsmall&avatarId=10142","32x32":"https://clientname.atlassian.net/secure/useravatar?size=medium&avatarId=10142"},"displayName":"John","active":true,"timeZone":"US"},"status":{"self":"https://clientname.atlassian.net/rest/api/2/status/1","description":"The问题是开放的,并准备好受让人开始工作it.","iconUrl":"https://clientname.atlassian.net/images/icons/statuses/open.png","name":"Open","id":"1","statusCategory":{"self":"https://clientname.atlassian.net/rest/api/2/statuscategory/2","id":2,"key":"new","colorName":"blue-gray","name":"To做}}}}]}我想解析器方法对上述JSON字符串来获得这些字段值的特定值,如:
摘要,状态,受让人, 意见,说明,创造,交货期,环境,优先级,项目, 记者,决议,issuetype,更新,选票,组件
我试图让一个又one.however,其复杂和时间服用。
这就是我正在努力。但错误:未将对象引用设置到对象的实例
私人列表&LT; JiraIssues&GT; GetIssues(SearchResponse响应)
{
名单&LT; JiraIssues&GT; returnResulttoReport =新的名单,其中,JiraIssues&GT;();
这里的foreach(在response.IssueDescriptions VAR项目)//错误(不知道为什么IssueDescriptions为NULL,但电话之前,我正在反序列化的话)
{
的foreach(在item.Comments VAR ITEM1)
{
}
}
返回returnResulttoReport;
}
我的返回类型是名单,其中,Issuees&GT;
。是否有任何库或示例的链接做到这一点?
请注意,我已经反序列化它,现在我想perticular列表,that.Also,请注意,我已经给了JSON字符串完全不一样,实际我have.But其相似。请指导。
使用类似 json2csharp 生成C#类JSON的,以prevent错误。然后像做
VAR的结果= JsonConvert.DeserializeObject&LT; RootObject&GT;(myJsonString);
修改
这里是如何得到摘要一个例子:
RootObject结果= JsonConvert.DeserializeObject&LT; RootObject&GT;(JSON);
名单&LT;字符串&GT;摘要=新的名单,其中,串&GT;();
的foreach(在result.issues VAR项)
{
VAR总结= item.fields.summary;
Console.WriteLine(摘要);
summaries.Add(摘要);
}
RootObject将重新present你JSON的顶层对象,然后它会对应到每个JSON的属性的属性。
I have JSON string returned by method:
public List<Issues> Getdata(
string jql,
List<string> fields = null,
int startAt = 0,
int maxResult = 500)
{
string data = JsonConvert.SerializeObject(request);
string result = runQuery(JiraResource.search, data, "POST"); //returns results and working fine
SearchResponse response = JsonConvert.DeserializeObject<SearchResponse>(result);
return response.IssueDescriptions = GetIssues(response);;
}
and JSON string look like this:
{"expand":"schema,names","startAt":0,"maxResults":50,"total":139,"issues":[{"expand":"operations,versionedRepresentations,editmeta,changelog,transitions,renderedFields","id":"20930","self":"https://clientname.atlassian.net/rest/api/2/issue/20930","key":"-139","fields":{"summary":"Rebate","issuetype":{"self":"https://clientname.atlassian.net/rest/api/2/issuetype/1","id":"1","description":"A fee negotiation or quote","iconUrl":"https://clientname.atlassian.net/images/icons/issuetypes/sales.png","name":"Fees","subtask":false},"components":[],"created":"2015-07-10T12:29:05.000+1000","description":"Execute as per attached instruction","project":{"self":"https://clientname.atlassian.net/rest/api/2/project/10013","id":"10013","key":"--","name":"Change Instructions","avatarUrls":{"48x48":"https://clientname.atlassian.net/secure/projectavatar?avatarId=10011","24x24":"https://clientname.atlassian.net/secure/projectavatar?size=small&avatarId=10011","16x16":"https://clientname.atlassian.net/secure/projectavatar?size=xsmall&avatarId=10011","32x32":"https://clientname.atlassian.net/secure/projectavatar?size=medium&avatarId=10011"}},"reporter":{"self":"https://clientname.atlassian.net/rest/api/2/user?username=rajap","name":"rajap","key":"rajap","emailAddress":"email.au","avatarUrls":{"48x48":"https://clientname.atlassian.net/secure/useravatar?avatarId=10142","24x24":"https://clientname.atlassian.net/secure/useravatar?size=small&avatarId=10142","16x16":"https://clientname.atlassian.net/secure/useravatar?size=xsmall&avatarId=10142","32x32":"https://clientname.atlassian.net/secure/useravatar?size=medium&avatarId=10142"},"displayName":"Prabu","active":true,"timeZone":"US"},"priority":{"self":"https://clientname.atlassian.net/rest/api/2/priority/3","iconUrl":"https://clientname.atlassian.net/images/icons/priorities/major.png","name":"Medium","id":"3"},"resolution":null,"duedate":"2015-07-15","comment":{"startAt":0,"maxResults":1,"total":1,"comments":[{"self":"https://clientname.atlassian.net/rest/api/2/issue/20930/comment/24290","id":"24290","author":{"self":"https://clientname.atlassian.net/rest/api/2/user?username=acahill","name":"acahill","key":"acahill","emailAddress":"email","avatarUrls":{"48x48":"https://clientname.atlassian.net/secure/useravatar?avatarId=10142","24x24":"https://clientname.atlassian.net/secure/useravatar?size=small&avatarId=10142","16x16":"https://clientname.atlassian.net/secure/useravatar?size=xsmall&avatarId=10142","32x32":"https://clientname.atlassian.net/secure/useravatar?size=medium&avatarId=10142"},"displayName":"Andrew Cahill","active":true,"timeZone":"US"},"body":"John, please action","updateAuthor":{"self":"https://clientname.atlassian.net/rest/api/2/user?username=acahill","name":"acahill","key":"acahill","emailAddress":"email","avatarUrls":{"48x48":"https://clientname.atlassian.net/secure/useravatar?avatarId=10142","24x24":"https://clientname.atlassian.net/secure/useravatar?size=small&avatarId=10142","16x16":"https://clientname.atlassian.net/secure/useravatar?size=xsmall&avatarId=10142","32x32":"https://clientname.atlassian.net/secure/useravatar?size=medium&avatarId=10142"},"displayName":"Andrew","active":true,"timeZone":"US"},"created":"2015-07-10T12:35:43.728+1000","updated":"2015-07-10T12:35:43.728+1000"}]},"votes":{"self":"https://clientname.atlassian.net/rest/api/2/issue/-139/votes","votes":0,"hasVoted":false},"assignee":{"self":"https://clientname.atlassian.net/rest/api/2/user?username=olearyj","name":"olearyj","key":"olearyj","emailAddress":"email","avatarUrls":{"48x48":"https://clientname.atlassian.net/secure/useravatar?avatarId=10142","24x24":"https://clientname.atlassian.net/secure/useravatar?size=small&avatarId=10142","16x16":"https://clientname.atlassian.net/secure/useravatar?size=xsmall&avatarId=10142","32x32":"https://clientname.atlassian.net/secure/useravatar?size=medium&avatarId=10142"},"displayName":"John","active":true,"timeZone":"US"},"status":{"self":"https://clientname.atlassian.net/rest/api/2/status/1","description":"The issue is open and ready for the assignee to start work on it.","iconUrl":"https://clientname.atlassian.net/images/icons/statuses/open.png","name":"Open","id":"1","statusCategory":{"self":"https://clientname.atlassian.net/rest/api/2/statuscategory/2","id":2,"key":"new","colorName":"blue-gray","name":"To Do"}}}}]}
I wanted parser method for the above json string to get particular values of these field values like :
"summary", "status", "assignee", "comments", "description", "created", "duedate", "environment", "priority", "project", "reporter", "resolution", "issuetype", "Updated", "votes", "components"
I tried to get one by one.however, its complex and time taking.
This is how I'm trying. But error: Object reference not set to an instance of an object.
private List<JiraIssues> GetIssues(SearchResponse response)
{
List<JiraIssues> returnResulttoReport = new List<JiraIssues>();
foreach (var item in response.IssueDescriptions) //error here (not sure why IssueDescriptions is NULL but before call i'm de-serializing it)
{
foreach (var item1 in item.Comments)
{
}
}
return returnResulttoReport;
}
My return type is List<Issuees>
. Is there any library or example link to do this?
Please note that, I'm already de-serializing it, now I wanted perticular list from that.Also, please note that the JSON full string I have given is not same as the actual I have.But its similar. Please guide.
Use something like json2csharp to generate C# classes for the json, to prevent errors. Then do something like
var result = JsonConvert.DeserializeObject<RootObject>(myJsonString);
EDIT
Here is an example of how to get the summaries:
RootObject result = JsonConvert.DeserializeObject<RootObject>(json);
List<string> summaries = new List<string>();
foreach (var item in result.issues)
{
var summary = item.fields.summary;
Console.WriteLine(summary);
summaries.Add(summary);
}
RootObject will represent the top level object of your JSON and then it will have properties which correspond to each of the properties of your JSON.
这篇关于从更大的JSON字符串检索所需的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!