Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to parse this json file and write separate sub object as json files? #392

Closed
panovr opened this issue Dec 11, 2016 · 1 comment
Closed

Comments

@panovr
Copy link

panovr commented Dec 11, 2016

My json file like:

[
    {
        "annotations": [
            {
                "class": "bbox",
                "height": 457.0,
                "id": "001",
                "type": "rect",
                "width": 336.0,
                "x": 97.0,
                "y": 60.0
            },
            {
                "class": "head",
                "height": 114.0,
                "id": "001",
                "type": "rect",
                "width": 208.0,
                "x": 222.0,
                "y": 65.0
            },
        ],
        "class": "image",
        "filename": "001.jpg"
    },
   {
        "annotations": [
            {
                "class": "bbox",
                "height": 406.0,
                "id": "001",
                "type": "rect",
                "width": 311.0,
                "x": 108.0,
                "y": 147.0
            },
            {
                "class": "head",
                "height": 107.0,
                "id": "001",
                "type": "rect",
                "width": 187.0,
                "x": 113.0,
                "y": 152.0
            },
        ],
        "class": "image",
        "filename": "002.jpg"
    },
]

You can see its document root is an Array object, and there are 2 separate objects in it. Now I want to create separate Document with every object as root, and write these Documents as separate json files. How can I achieve this?
Thanks!

@panovr panovr changed the title How to parse this json file and write separate sub object to files? How to parse this json file and write separate sub object as json files? Dec 11, 2016
@panovr
Copy link
Author

panovr commented Dec 12, 2016

just use the these codes for reading and writing:

// read a JSON file
std::ifstream i("file.json");
json j;
i >> j;

// write prettified JSON to another file
std::ofstream o("pretty.json");
o << std::setw(4) << j << std::endl;

The API is designed well, and easy to use.
Thanks for sharing this project!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants