dropbox / json11

A tiny JSON library for C++11.
MIT License
2.54k stars 613 forks source link

How do I parse a json file ? #116

Closed shakthi-prashanth-m closed 6 years ago

shakthi-prashanth-m commented 6 years ago

Hi, My program need to parse a json file whose contents look like this:

{
    "fileType": "Plan",
    "geoFence": {
        "polygon": [
        ],
        "version": 1
    },
    "groundStation": "QGroundControl",
    "mission": {
        "cruiseSpeed": 15,
        "firmwareType": 12,
        "hoverSpeed": 5,
        "items": [
            {
                "autoContinue": true,
                "command": 178,
                "doJumpId": 1,
                "frame": 2,
                "params": [
                    1,
                    15,
                    -1,
                    0,
                    0,
                    0,
                    0
                ],
                "type": "SimpleItem"
            },
            {
                "autoContinue": true,
                "command": 22,
                "doJumpId": 2,
                "frame": 3,
                "params": [
                    0,
                    0,
                    0,
                    0,
                    47.39784887,
                    8.54554763,
                    15
                ],
                "type": "SimpleItem"
            },
            {
                "autoContinue": true,
                "command": 16,
                "doJumpId": 3,
                "frame": 3,
                "params": [
                    0,
                    0,
                    0,
                    null,
                    47.39780711,
                    8.54567906,
                    15
                ],
                "type": "SimpleItem"
            },
            {
                "autoContinue": true,
                "command": 178,
                "doJumpId": 4,
                "frame": 2,
                "params": [
                    1,
                    15,
                    -1,
                    0,
                    0,
                    0,
                    0
                ],
                "type": "SimpleItem"
            },
            {
                "autoContinue": true,
                "command": 16,
                "doJumpId": 5,
                "frame": 3,
                "params": [
                    0,
                    0,
                    0,
                    null,
                    47.3977245,
                    8.54558116,
                    15
                ],
                "type": "SimpleItem"
            },
            {
                "autoContinue": true,
                "command": 178,
                "doJumpId": 6,
                "frame": 2,
                "params": [
                    1,
                    15,
                    -1,
                    0,
                    0,
                    0,
                    0
                ],
                "type": "SimpleItem"
            },
            {
                "autoContinue": true,
                "command": 16,
                "doJumpId": 7,
                "frame": 3,
                "params": [
                    0,
                    0,
                    0,
                    null,
                    47.3977009,
                    8.54545644,
                    15
                ],
                "type": "SimpleItem"
            },
            {
                "autoContinue": true,
                "command": 178,
                "doJumpId": 8,
                "frame": 2,
                "params": [
                    1,
                    15,
                    -1,
                    0,
                    0,
                    0,
                    0
                ],
                "type": "SimpleItem"
            }
        ],
        "plannedHomePosition": [
            47.3977435,
            8.5454669,
            488.138
        ],
        "vehicleType": 2,
        "version": 2
    },
    "rallyPoints": {
        "points": [
        ],
        "version": 1
    },
    "version": 1
}

How do I parse this file? I am especially interested in mission.items. Appreciate your help. Thanks.

artwyman commented 6 years ago

Json11 parses strings not files. See Json::parse(). Reading a string into a file is up to you.

Closing out since this isn't really an issue, just a question.

shakthi-prashanth-m commented 6 years ago

Thanks @artwyman. Yeah, I shall read file into a string and use Json::parse()