用C++从文件中提取JSON数据

1 c++ string json file

在这里,对不起,如果这个问题不适合这个论坛。我对编程很陌生,并认为通过创建这个小项目我可以更好地控制字符串和文件。我想要做的是从 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}
Run Code Online (Sandbox Code Playgroud)

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

bla*_*esa 5

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

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

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

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

    "body" : "none"

}
Run Code Online (Sandbox Code Playgroud)

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

 #include "boost/property_tree/ptree.hpp"
 #include "boost/property_tree/json_parser.hpp"
Run Code Online (Sandbox Code Playgroud)

然后这里是最有趣的部分:

 boost::property_tree::ptree root;
Run Code Online (Sandbox Code Playgroud)

您创建名为 的 ptree 对象root

boost::property_tree::read_json("/path_to_my_file/doc.json", root);
Run Code Online (Sandbox Code Playgroud)

然后你告诉你要读取什么文件,以及在哪里存储它(这里是 root)。小心,如果try / catch文件不存在,你应该使用它。

然后您将只使用root非常容易做到的树。您有很多功能(我邀请您查看 boost 文档页面)。

您想访问该name字段。那么就这样做:

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

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

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

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

这有点复杂。我解释。你告诉 boost 用 , 来查看 root 的每个孩子root.get_child("")""用于 root。然后,对于找到的每个子项(如基本迭代器),您将使用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