Search code examples
androidjsonfacebookcocos2d-xcocos2d-android

Reading Facebook Response With Jannson JSON Reader


So long story short I have a android app and I used cocos2dx to dev it. One component I am working on is bringing my facebook friends into my game. The way I did it was on the native side (java) I setup my facebook sdk. I succefuly login and pull down my friends list without problems. My issue is that I need to forward this data to the c++ side so I can access the data and bring it into labels etc..

Here I guess some structure of how stuff is happening: Java native - start activity, login to facebook, get friends -> STRING DATA JNI TO C++ -> CPP parse JSON data with Jannson.

My issue is that if I have a sample data like this:

[
    {
        "pic_square": "https://www.facebook.com/blah",
        "uid": 4654546445,
        "name": "somename"
    }
]

I can parse that no problem, But in reality what facebook response with is something like this:

{
    Response: responseCode: 200,
    graphObject: GraphObject{
        graphObjectClass=GraphObject,
        state={
            "data": [
                {
                    "pic_square": "https://www.facebook.com/blah",
                    "uid": 4654546445,
                    "name": "somename"
                }
            ]
        }
    }
}

And with that Jansson fails stating that its not an array (exact error is "error: root is not an array"). Not sure how to handle this. Should I be somehow parsing out the stuff after "data": and then figuring out where to stop correctly or is there a better way.

Thanks!!


Solution

  • What you'll need to do is modify the parsing logic to first handle the Json objects that wrap the data array you're interested in. Although this will require some extra programming, it definitely beats any String manipulation attempts. Unless you're a 100% sure that "[" and "]" will always be part of the response, then I wouldn't be making any assumptions about what you're receiving.

    I'm not familiar with Jannson, but you'll want to do some other bits and pieces before handling the data array. Just from looking at the tutorial, it should probably look somewhat like this:

    // make request
    text = request(url);
    // decode json
    root = json_loads(text, 0, &error);
    
    // parse "Response"
    json_t *response = json_object_get(root, "Response");
    json_t *responseCode = json_object_get(response, "responseCode");
    int responseCodeValue = json_integer_value(responseCode);
    
    // parse "graphObject"
    json_t *graphObject = json_object_get(root, "graphObject");
    json_t *graphObjectClass = json_object_get(graphObject, "graphObjectClass");
    json_t *state = json_object_get(graphObject, "state");
    json_t *data = json_object_get(state, "data");
    
    // iterate over the "data" array, parse the key/values etc.
    for(i = 0; i < json_array_size(data); i++) {
        json_t *data = json_array_get(root, i);
    }
    

    For the sake of this example, I've omitted all type checks (you will want to add those yourself) as well as any cleaning up of memory/variables. Also, please beware of any typos and/or obvious mistakes, as I simply typed this straight into the browser and did not do any compile or runtime checks. I'm sure you'll be able to filter those out on your own.

    One thing that I'm curious about is why you've opted for Jannson? I'm guessing because of its support for both Android and iOS? If you're specifically targeting Android, there are lots of other options out there. For example, basic Json support is built into the Android framework, but there's also 3rd party libraries that will enable mapping of Json to Java objects, like GSON and Jackson.