gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 12:25:24 25 4
gpt4 key购买 nike

我有方法返回的 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 字符串的解析器方法来获取这些字段值的特定值,例如:

“摘要”,“状态”,“受让人”, “评论”,“描述”,“创建”,“截止日期”,“环境”,“优先级”,“项目”, "reporter", "resolution", "issuetype", "Updated", "votes", "components"

我试图一个一个地得到。但是,它很复杂而且很费时间。

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

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<Issuees> .是否有任何库或示例链接可以执行此操作?

请注意,我已经反序列化了它,现在我想要它的特定列表。另外,请注意,我提供的 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 的每个属性的属性。

关于c# - 从更大的 JSON 字符串中检索所需的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31531441/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com