How to manage Facebook api response data
I am developing an app for iPhone and I am sending this url to facebook: https://graph.facebook.com/search?type=checkin&access_token=ACCESS_TOKEN
and the facebook reply 开发者_JAVA百科something like this:
{
"data": [
{
"id": "2081746282701",
"from": {
"name": "USER1",
"id": "xxxxxxxxxx"
},
"place": {
"id": "xxxxxxxxx",
"name": "\u03a0\u03b1\u03bb\u03bb\u03ac\u03b4\u03b9\u03bf",
"location": {
"city": "xxxxxxxx",
"country": "Greece",
"latitude": xxxxx,
"longitude":xxxxx
}
},
"application": {
"name": "Facebook for iPhone",
"id": "6628568379"
},
"created_time": "2011-05-25T01:22:00+0000",
"likes": {
"data": [
{
"id":"xxxxxx",
"name": TaggedUsers"
},
{
"id": "xxxxxx",
"name": "tagged user"
}
]
}
},
{
"id": "xxxxxxx",
"from": {
"name": "UserWhich puplish this",
"id": "740646984"
},
"tags": {
"data": [
{
"name": "XXXXXX",
"id": "xxxxxxxxxx"
}
]
},
"place": {
"id": "xxxxxxxxxxxxx",
"name": "xxxxxxxx",
"location": {
"latitude": xxxxxx,
"longitude": xxxxxx
}
},
"application": {
"name": "Facebook for Android",
"id": "350685531728"
},
"created_time": "2011-05-23T17:18:54+0000",
"likes": {
"data": [
{
"id": "xxxxxxx",
"name": "xxxxxxxxx"
}
]
}
}
}
My question is how I will manage this string to export long lat place and the user who checked in? I think that NSCaseInsensitiveSearch wont do the trick because there is no unique variable to search for. For example the "data" is placed at the start of the responsed string and before of each check in but its placed and before tagged user!
This is JSON: JavaScript Object Notation.
You can decode it easily into a NSDictionary or NSArray with the json-framework depending on the root element. After that it's a tree like structure depending on the JSON returned.
The mentioned framework provides an easy to use category for NSObject which offers the method JSONValue.
Example to decode the json you'v received:
NSString *myJson = @"...";
NSDictionary *decodedJson = [myJson JSONValue];
Now you can use fast enumeration or direct access(objectForKey:
) to retrieve the data you are interested in.
NSArray *users = [decodedJson objectForKey:@"data"];
foreach(NSDictionary *user in users =) {
NSLog(@"user's id: %@", [user objectForKey:@"id"]);
NSDictionary *fromData = [user objectForKey:@"from"]);
NSLog(@"user is from: %@", [fromData objectForKey:@"name"]);
NSLog(@"user is from id: %@", [fromData objectForKey:@"id"]);
// and so on ..
}
精彩评论