从字符串开头查找特定数字的单词

Find specific number of a word from the beginning in string

我一直在使用来自我的 jira 的 api 调用来收集信息。收集到的信息保存在正文文件中,内容如下:

没有门票:

{"startAt":0,"maxResults":50,"total":0,"issues":[]}{"startAt":0,"maxResults":50,"total":0,"issues":[]}

一张票:

{"expand":"names,schema","startAt":0,"maxResults":50,"total":1,"issues":[{"expand":"operations,versionedRepresentations,editmeta,changelog,renderedFields","id":"456881","self":"https://myjira...com","key":"TICKET-1111","fields":{"summary":"[TICKET] New Test jira","created":"2018-12-17T01:47:09.000-0800"}}]}{"expand":"names,schema","startAt":0,"maxResults":50,"total":1,"issues":[{"expand":"operations,versionedRepresentations,editmeta,changelog,renderedFields","id":"456881","self":"https://myjira...com","key":"TICKET-1111","fields":{"summary":"[TICKET] New Test jira","created":"2018-12-17T01:47:09.000-0800"}}]}

两张票:

{expand:schema,names,startAt:0,maxResults:50,total:2,issues:[{expand:operations,versionedRepresentations,editmeta,changelog,renderedFields,id:456881,self:https://myjira...com,key:TICKET-1111,fields:{summary:[TICKET] New Test jira,created:2018-12-17T01:47:09.000-0800}},{expand:operations,versionedRepresentations,editmeta,changelog,renderedFields,id:320281,self:https://myjira...com,key:TICKET-2222,fields:{summary:[TICKET] Test jira,created:2016-03-18T07:58:52.000-0700}}]}{expand:schema,names,startAt:0,maxResults:50,total:2,issues:[{expand:operations,versionedRepresentations,editmeta,changelog,renderedFields,id:456881,self:https://myjira...com,key:TICKET-1111,fields:{summary:[TICKET] New Test jira,created:2018-12-17T01:47:09.000-0800}},{expand:operations,versionedRepresentations,editmeta,changelog,renderedFields,id:320281,self:https://myjira...com,key:TICKET-2222,fields:{summary:[TICKET] Test jira,created:2016-03-18T07:58:52.000-0700}}]}

等..

使用此代码,我已经能够收集到所有公开票:

std::ifstream t("BodyOpenIssues.out");
std::string BodyString((std::istreambuf_iterator<char>(t)),
    std::istreambuf_iterator<char>());
// Removing Quotes
BodyString.erase(std::remove(BodyString.begin(), BodyString.end(), '"'), BodyString.end());
int Result = 0;
unsigned first = BodyString.find("total:");
unsigned last = BodyString.find(",issues");
std::string TotalOpenIssues = BodyString.substr(first + 6, last - (first + 6));
Result = std::stoi(TotalOpenIssues);

return Result;

使用第二个函数,我试图根据未结门票总数获取钥匙。

if (GetOpenIssuesNumber() > 0)
{
    std::ifstream t("BodyOpenIssues.out");
    std::string BodyString((std::istreambuf_iterator<char>(t)),
        std::istreambuf_iterator<char>());
    // Removing Quotes
    BodyString.erase(std::remove(BodyString.begin(), BodyString.end(), '"'), BodyString.end());
    unsigned first = BodyString.find("key:TICKET-");
    unsigned last = BodyString.find(",fields");
    std::string TotalOpenIssues = BodyString.substr(first + 11, last - (first + 11));
    String^ Result = gcnew String(TotalOpenIssues.c_str());
    return "TICKET-" + Result;
}
else
{
    return "No open issues found";
}

我的意思是: 如果 Total 为 1 则从头开始查找,找到第一个键 TICKET-1111。 如果 Total 为 2,则从头搜索并获得第一个密钥 TICKET-1111,然后从那里继续查找下一个密钥 TICKET-2222。 并根据该总数在该字符串中找到那么多键。

当 ifstream 读取文件并将结果保存在 std::string 中时,我迷失了类型之间的所有转换。找到后我将结果保存在 System::String 中以在我的标签中使用它。我一直在研究并发现我可以使用 char 数组但我不能根据 BodyString.length 使其动态化().

如果需要更多信息,请告诉我。 非常感谢任何建议!提前致谢!

我去了 nlohmann json 图书馆。它拥有我需要的一切。谢谢核桃!

These are formatted as JSON. You should use a JSON library for C++ and parse the files with that. Using search/replace is unnecessary complicated and you will likely run into corner cases you haven't considered sooner or later (do you really want the code to randomly miss tickets, etc.?). Also String^ is not C++. Are you writing C++/CLI instead of C++? If so, please tag c++-cli instead of c++. – walnut