0

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!!

kub
  • 93
  • 1
  • 5
  • Any chance you just copy-pasted some code from the [Jannson tutorial](https://github.com/akheron/jansson/blob/master/doc/tutorial.rst)? That example works with a Json array (as does your first snippet; indicated by the square brackets). However, the Facebook response is just a single object. It does contain an array (`data`) at some point down the hierarchy, but you'll need to parse the top-level stuff first to get there. Basically you're now trying to map a single object onto an array - which is really what the error states too. – MH. Mar 24 '13 at 18:18
  • Yeah I took the code from the Jannson tutorial I jsut modified it a tiny bit to extract integers. The tutorial only grabs out strings. So I guess I'll need to figure out how to just grab the stuff after "data: [...]" and make sure I stop at the correct bracket. Anyways thanks for the reply!! – kub Mar 24 '13 at 18:33
  • Not a facebook expert but would it be safe for that particular call / response to set the string delimiters to equal to "]" and "[" to extract the data. .. Is that too dangerous? – kub Mar 24 '13 at 22:50

1 Answers1

1

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.

MH.
  • 45,303
  • 10
  • 103
  • 116