在这里,对不起,如果这个问题不适合这个论坛。我对编程很新,并且认为通过创建这个小项目我可以更好地掌握字符串和文件。我要做的是从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''发现'它有可能跳过npos并且循环继续。添加if语句修复它,但使代码看起来很笨重。必须有一个更优雅的解决方案。 提前谢谢!
答案 0 :(得分:3)
我建议您使用第三方来完成所有这些工作,这对于原始工具来说非常困难。我最近做了这种事情,所以我可以给你一些帮助。
我建议你看一下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中)。请注意,如果文件不存在,您应该使用 然后你只会使用真正容易做到的 您想要访问 让我们完成另一个例子。假设您要检查所有根节点,这意味着位于顶层的每个节点。 这有点复杂。我解释。您告诉boost使用 因此,在 我邀请您查看Boost文档。一开始看起来可能很难,但之后很容易使用。 http://www.boost.org/doc/libs/1_41_0/doc/html/property_tree.html root
的boost::property_tree::read_json("/path_to_my_file/doc.json", root);
try / catch
。root
树。您有许多功能(我邀请您查看增强文档页面)。name
字段。然后就这样做: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; }
root.get_child("")
查看根的每个子项,""
用于root。然后,对于找到的每个孩子(如基本迭代器),您将使用const boost::property_tree::ptree::value_type& child
。foreach
内,您将使用child
访问您想要的内容。 child.first
将为您提供当前正在使用的子节点的名称。在我的示例中,它将首先打印document
,然后打印body
。