1

Actually I am trying to parse a json file using rapidjson library . But when i am trying to add this header file in my code it shows me an error like this

"[Error] rapidjson/document.h: No such file or directory" and

"recipe for target 'main_1.o' failed"

here main_1 is my file name. This is my actual code

#include<stdio.h>
#include "rapidjson/document.h"

using namespace rapidjson;

Document document;
document.Parse(json);

int main()
{
    char name[50];
    int t_value;
    return 0;
    
}

And also i haven't idea about where i want to add my json file?

But i really don't know where i did a mistake? please anyone help me.

klutt
  • 30,332
  • 17
  • 55
  • 95
Ramya T
  • 19
  • 1
  • 2
  • *How* did you install rapidjson? *Where* did you install it? – Some programmer dude Sep 22 '20 at 10:47
  • Also remember that you can't have general statements outside of functions. It seem you might need to take a couple of steps back and go over some of the basics of C again. – Some programmer dude Sep 22 '20 at 10:47
  • 1
    Please show how you are building the code. Is it a Makefile? Direct compiler commands? Also, state where `rapidjson` has been installed. For starters, `document.Parse(json);` is invalid as C doesn't allow such expressions outside a function - can only be variable declarations, intialisations and function definitions and function prototypes. – kaylum Sep 22 '20 at 10:49
  • @Someprogrammerdude Actually i didn't install any rapidjson library. – Ramya T Sep 23 '20 at 16:16
  • @Someprogrammerdude Actually i didn't install any rapidjson library.Actually i want to practice with parsing json files using c language.Is it possible? If you feel free please tell me what i want to install and where? In c it is not possible means can i do it in c++? – Ramya T Sep 23 '20 at 16:25
  • `#include` is looking for a source file. Download RapidJSON here: https://github.com/Tencent/rapidjson/releases/tag/v1.1.0 , then copy the contents of include/rapidjson into your project so they can be found. Since RapidJSON is header-only, it should just work as long as the files are in the right place. – parktomatomi Sep 25 '20 at 05:10

2 Answers2

0

Kindly check the below link for installation of json, you can also visit rapidjson official website

RapidJson installation

And for your code: Download all header files of rapidjson and keep it inside your current folder under rapidjson folder(new folder)

Write the below code inside main, compiler error will occur due to this.

Document document;
document.Parse(json);
Mohit Jain
  • 270
  • 2
  • 6
0

If you are using Ubuntu then package manager can be used to install the rapidjson lib

$ sudo apt-get update
$ sudo apt-get install rapidjson-dev 

The path of the rapidjson include for me was

/usr/include/rapidjson

and in the cpp/hpp file

#include <rapidjson/document.h>

worked for me

sample program to load file

#include <iostream>
#include <fstream>
#include <cstdlib>

#include <rapidjson/document.h>
#include <rapidjson/istreamwrapper.h>
#include <rapidjson/writer.h>
#include <rapidjson/stringbuffer.h>
#include <rapidjson/ostreamwrapper.h>

int main()
{

    using namespace rapidjson;

    std::ifstream ifs { R"(myfile.json)" };
    if ( !ifs.is_open() )
    {
        std::cerr << "Could not open file for reading!\n";
        return EXIT_FAILURE;
    }

    IStreamWrapper isw { ifs };

    Document doc {};
    doc.ParseStream( isw );

    StringBuffer buffer {};
    Writer<StringBuffer> writer { buffer };
    doc.Accept( writer );

    if ( doc.HasParseError() )
    {
        std::cout << "Error  : " << doc.GetParseError()  << '\n'
                  << "Offset : " << doc.GetErrorOffset() << '\n';
        return EXIT_FAILURE;
    }

    const std::string jsonStr { buffer.GetString() };

    std::cout << jsonStr << '\n';



    std::cout <<"done\n";
    return EXIT_SUCCESS;
}

Demo code Source: How to read json file using rapidjson and output to std::string?

Deepak Sharma
  • 582
  • 8
  • 10