gpt4 book ai didi

c++ - 从 C++ 文件中提取 JSON 数据

转载 作者:行者123 更新时间:2023-11-28 00:28:35 24 4
gpt4 key购买 nike

在这里,抱歉,如果这个问题不适合这个论坛。我对编程还很陌生,我认为通过创建这个小项目我可以更好地掌握字符串和文件。我要做的是从 JSON 文档中提取数据。最终我会将数据存储在一个数组中,我想稍后再使用它。

基本上,我想知道是否有更好的方法来解决这个问题。代码看起来有点罗嗦而且绝对不优雅。再次抱歉,如果这个问题不是一个好问题,但我认为没有比通过这样的社区更好的学习方式了。

#include <iostream>
#include <fstream>
#include <cstring>
#include <string> //probably including more than necessary

using namespace std; //should be specifying items using scope resolution operator instead


int main(int argc, const char * argv[])
{

ifstream sfile("JSONdatatest.txt");
string line,temp;


while(!sfile.eof()){

getline(sfile, line);
temp.append(line); //creates string from file text, use of temp seems extraneous

}
sfile.close();


cout << "Reading from the file.\n";
size_t counter=0;
size_t found=0;
size_t datasize=0;

while(found!=string::npos && found<1000*70){ //problem here, program was creating infinite loop
//initial 'solution' was to constrain found var
//but fixed with if statement

found = temp.find("name: ",counter);
if(found!=string::npos){

found=found+7; //length of find variable "name: ", puts us to the point where data begins
size_t ended=temp.find_first_of( "\"", found);

size_t len=ended-found; //length of datum to extract


string temp2(temp, found, len); //odd use of a second temp function,
cout << temp2 << endl;
counter=ended+1;
datasize++; //also problem with data size and counter, so many counters, can they
//coordinate to have fewer?
}

}

cout << datasize;


return 0}

在我指示进行无限循环的地方,我通过在 while 循环中添加 if 语句来修复。我的猜测是因为我将 7 添加到“found”,它有可能跳过 npos 并继续循环。添加 if 语句修复了它,但使代码看起来笨拙。必须有一个更优雅的解决方案。提前致谢!

最佳答案

我建议您使用第三方来完成所有这些工作,这对于原始工具来说非常困难。我最近实际上做了这种事情,所以我可以给你一些帮助。

我建议您看看 boost::property_tree .这是理论:Json 文件就像一棵树,有一个根和许多分支。想法是将此 JSON 文件转换为 boost::property_tree::ptree ,那么您就可以轻松使用对象 ptree而不是文件。

首先,假设我们有这个 JSON 文件:

{
"document": {
"person": {
"name": "JOHN",
"age": 21
},
"code": "AX-GFD123"
}

"body" : "none"

}

然后在你的代码中,一定要包括:

 #include "boost/property_tree/ptree.hpp"
#include "boost/property_tree/json_parser.hpp"

接下来是最有趣的部分:

 boost::property_tree::ptree root;

您创建名为 root 的 ptree 对象.

boost::property_tree::read_json("/path_to_my_file/doc.json", root);

然后您告诉要读取的文件,以及将其存储在何处(此处为根目录)。小心,你应该使用 try / catch以防文件不存在。

那么你将只使用 root这真的很容易做的树。您有很多功能(我邀请您查看 boost 文档页面)。

您想访问 name field 。然后就这样做:

std::string myname = root.get<std::string> ("document.person.name", "NOT FOUND");

get函数的第一个参数是获取所需属性的路径,第二个参数用于在路径不正确或不存在时默认返回。 <std::string>是为了显示它必须返回什么类型。

让我们以另一个例子作为结束。假设您要检查所有根节点,这意味着顶层的每个节点。

  BOOST_FOREACH(const boost::property_tree::ptree::value_type& child, root.get_child(""))
{ cout << child.first << endl; }

这有点复杂。我解释。你告诉 boost 使用 root.get_child("") 查看根的每个 child , ""用于根。然后,对于找到的每个 child (如基本迭代器),您将使用 const boost::property_tree::ptree::value_type& child .

所以在foreach里面,您将使用 child访问任何你想要的。 child.first将为您提供当前正在使用的子节点的名称。在我的示例中,它将首先打印 document , 然后 body .

我邀请您查看 Boost 文档。一开始看起来可能很难,但之后真的很容易使用。

http://www.boost.org/doc/libs/1_41_0/doc/html/property_tree.html

关于c++ - 从 C++ 文件中提取 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23860622/

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