How to read a JSON file containing multiple root elements?

If I have a file whose contents looked like this:

{"one": 1}
{"two": 2}

I could just parse every single line as a separate JSON object (using JsonCpp ). But what if the file structure was less convenient, for example:

{
   "one":1
}

{
   "two":2
}
+5
source share
2 answers

None of the examples in your question is a valid JSON object; A JSON object can have only one root. You must split the file into two objects and then analyze them.

You can use http://jsonlint.com to find out if a given string is valid JSON or not.

, - JSON , , JSON.

, , , .

JSON:

{
    "one": 1,
    "two": 2
}

, :

{
    "one":
    {
        "number": 1
    },
    "two":
    {
        "number": 2
    }
}
+5

. . json lib . QT.

+2

All Articles