How to read a file of json?C++

I don’t know how to read a file of json by using c++.
My English is so-so,hehe.
Who can give me an example.

Hi!

I use the Jansson library (http://www.digip.org/jansson/). It works like a charm on both ios and android and the API is pretty well documented (http://www.digip.org/jansson/doc/2.4/) :wink:

Here is a simple example on how to load and parse a JSON document.

// Load the JSON document
const char* fullpath = NULL;
unsigned long filesize;
unsigned char* data;
int flags = 0;
json_error_t error;

fullpath = CCFileUtils::sharedFileUtils()->fullPathFromRelativePath("level1.json");
data = CCFileUtils::sharedFileUtils()->getFileData(fullpath, "r", &filesize);
root = json_loadb((char*)data, filesize, flags, &error);
if( root == NULL ) {
  dbg_parser_error("Error parsing the json document on line %d: %s %s", error.line, error.text);
  return false;
}

// Iter on a JSON array of "body" objects  
json_t* bodies = json_object_get(root, "bodies");

for( int i=0; i < json_array_size(bodies); i++)
{
    // Get the current "body" object
    json_t* body = json_array_get(bodies, i);
    // Get the body´s properties
    json_t* json_angle = json_object_get(body, "angle");  
    int value = json_integer_value(json_angle); 
    ...
}

Hope this helps…

:smiley:

excellent work! Thanks!
den ranx wrote:

Hi!
>
I use the Jansson library (http://www.digip.org/jansson/). It works like a charm on both ios and android and the API is pretty well documented (http://www.digip.org/jansson/doc/2.4/) :wink:
>
Here is a simple example on how to load and parse a JSON document.
[…]
Hope this helps…

Hi,

I try to use jansson with android so, den ranx, when you wrote:

Hi!
>
I use the Jansson library (http://www.digip.org/jansson/). It works like a charm on both ios and android […]

it’s seem good news for me !

But could you explain how to compile jansson for android ? Because when I compile it normally (./configure; make; make install) and try to build my app with the ndk I get the error message : ‘incompatible target’.

a friend released that one:

Thanks ! It could be a solution but I find a way to compile jansson for android.
If an other “noob” of android come here : I’ve created a new android project in Eclipse configurated to be a library and I’ve cloned the git repository of jansson in a new directory “jni”. There is an Android.mk file in jansson’s sources so I’ve just ron ndk-build and the compatible library was created !

@Narah
Hi ! I’m that stupid noob !
Can you please give a stupid-friendly step-by-step description ? plz !