c# – 从更大的JSON字符串中检索所需的字符串

我有方法返回的
JSON字符串:

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

和JSON字符串看起来像这样:

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

我想要上面的json字符串的解析器方法来获取这些字段值的特定值,如:

“摘要”,“状态”,“受让人”,
                “评论”,“描述”,“创造”,“强制”,“环境”,“优先”,“项目”,
                “记者”,“决议”,“发行类型”,“更新”,“投票”,“组成部分”

我试图逐一得到它.但是,它的复杂性和时间.

这就是我的尝试方式.但是错误:对象引用未设置为对象的实例.

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

我的返回类型是List< Issue>.是否有任何库或示例链接来执行此操作?

请注意,我已经对它进行了反序列化,现在我想从那里获得特定的列表.另外,请注意我给出的JSON完整字符串与我的实际字符不同.但它的相似之处.请指导.

最佳答案 使用像
json2csharp这样的东西为json生成C#类,以防止错误.然后做类似的事情

var result = JsonConvert.DeserializeObject<RootObject>(myJsonString);

编辑
以下是如何获取摘要的示例:

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将表示JSON的顶级对象,然后它将具有与JSON的每个属性相对应的属性.

点赞