JSON is a very easy format to parse, so you generally don't need a library for it (that is one of its main benefits) - you can just make a quick one yourself.
If you want a library anyway, I don't know of any that fullfil your requirements exactly. If you are using the boost library anyway, the Boost.Property_Tree library provides a JSON parser, as well as a very usable interface to retrieve data from it. Its also a header-only template library - what you don't use doesn't add to your executable.
However, if you don't want a dependency of boost, the official JSON website ( http://www.json.org/ )has about 20 conforming JSON parsers for C++ and another 20 or so for C (see near the bottom of the page). You can probably find one you like from those ones.
The problem with boost is that it uses strings for all it's types. So if you use boost to generate a json file that is going to be used in javascript or something similar. You'll need to manually convert the values to their appropriate types.
You'll need to manually convert the values to their appropriate types.
I only really needed it as an intermediate format for my game engine; just something that didn't stop my work flow, easy to add onto, and quick to parse. I had no intent to use javascript (or anything remotely similar), and I plan on compiling it to a different format upon release, so this really didn't effect me much (Jansson handles the conversions anyway).
Jansson works great and I'd recommend it to anyone; it was trivial to set it up with cmake and compile it's library (quick as well), and I got a working solution in a matter of minutes with everything set up.
Well it is handy information, I ended up using boost without realizing it and when my plans changed and i ended up using the json files with javascript it was a bit too late to change api. I did look at some other libraries before using boost, some of them didn't allow the user to specify the order or just put them in a random/alphabetical order, which kind of mattered with some of the content.
Jansson works great and I'd recommend it to anyone; it was trivial to set it up with cmake and compile it's library (quick as well), and I got a working solution in a matter of minutes with everything set up.
That is just about almost every library out there that uses cmake. That is what cmake is for after all.
That is just about almost every library out there that uses cmake. That is what cmake is for after all.
Well, my only experience with cmake was when I previously built SFML 2.1 a while ago (I've been using SDL for years and I wanted to see what SFML was about), and there were tons of non-trivial errors with the generation of the project file. I guess I just wasn't aware that it was reliable (which I don't think anymore).
I use cJSON - very lightweight and no setup required, just add cjson.c and cjson.h to your project and you are done. http://sourceforge.net/projects/cjson/
I like json-parser because of the granularity in the type system - it is much more fine-grained than JavaScript's type system. I actually needed to port a project to HTML5 and ran into a snag when I found out that JavaScript did not have disparate floating point and integer types. I think it also allows for duplicate keys in objects, which is useful for parsing user-made JSON.
If you don't care about the types of data (e.g. only care if it is a number or not) then the only real benefit of json-parser is the speed and low memory usage (maybe you're parsing a huge amount of data).
If you don't need the speed and don't care about excess memory usage, then use a high level C++ json-parsing library.
@LowestOne: Your first example was not valid JSON. Objects cannot contain nameless members. Your second example is also not valid JSON - array elements cannot have names.