How to Separate HTML Response - iphone

I am using FConnect in my app. After the user logs in to his/her Facebook account I am getting his/her information as:
{
"id":"###########",
"name":"Vishnu Gupta",
"first_name":"Vishnu",
"last_name":"Gupta",
"link":"facebook a/c link id=#######",
"education":[{"school":{"id":"######","name":"st.joseph"},"type":"High School"}],
"gender":"male","email":"##########",
"timezone":5.5,
"locale":"en_US",
"verified":true,
"updated_time":"2010-11-27T10:10:25+0000"
}
I want to store user's information in database. To do that, I have to separate the user's information that is id, name, and email.
I tried using componentsSeparatedByString method but I do not understand how to retrieve the information in an array.
Can anyone help me????

The response you are getting is in JSON Format which basically have response in Array & Dictionary.
You will be getting this data in Dictionary..so you can access it by using the key value..
NSLog(#"%#",[NSDictionaryObject valueforkey:#"id"]);
NSLog(#"%#",[NSDictionaryObject valueforkey:#"name"]);
NSLog(#"%#",[NSDictionaryObject valueforkey:#"first_name"]);
Hope this will surely work for you.....

ya.....its corrrect.In detail:
Add JSON SDK then write the below code
SBJSON *json = [[SBJSON alloc] init];
NSDictionary *returnObject = [json objectWithString:InfoName];
NSString #id=[NSString stringWithFormat:#"%#",[returnObject objectForKey:#"id"]];
same for name ,email.
then Add these objects to Array and release the json
JSOn is good to use for "key-value' pair parsing.

Related

Creating Objects from JSON

I would like to be able to create an NSArray or NSDictionary with a JSON string that I receive from a web service. I am using the json framework to do this. The response string will look something along these lines:
{count:2,
data:[{"ID":8,
"Title":"Test Title",
"Author":"Test Author",
"Price":"18.00",
"Edition":"1st",
"Condition":"Good",
"UploadDate":"2012-07-28 07:25:56.0"
},
{"ID":9,
"Title":"Test Title",
"Author":"Test Author",
"Price":"18.000000000000",
"Edition":"1st",
"Condition":"Good",
"UploadDate":"2012-07-28 07:27:06.0"
}
]
}
My question is, what is the most efficient way to grab all of the data from the 'data' array and use it to create either an NSArray or NSDictionary? Any help would be much appreciated
I use RestKit for JSON parsing. It has all you need to fetch the json from a server, translate it into a dictionary, and from there into an object that you can work with. That might be more than you dared to wish for, but it works like a charm. Check out https://github.com/RestKit/RestKit for more info.
Starting from iOS5 a json parser has been integrated as a SDK native component see the following link http://www.raywenderlich.com/5492/working-with-json-in-ios-5.
You will create a NSDictionary with 2 keys "count" and "data" and then you will get the NSArray using objectForKey with the "data" key.

RestKit - Appending a parameter to all requests

I have an iOS app using RestKit in order to communicate with a RESTful Rails server. The server uses a basic session token for authentication of users. I want to append this token to every request sent using the RKObjectManager's methods.
I've tried creating a Category overload of NSManagedObject in order to use the following method:
- (void)willSendWithObjectLoader:(RKObjectLoader *)objectLoader
That works fine, however I see no way of appending to the object loader's params. I've even gone as far as to reserialize the object but there is no way to do that without it being sent using escape characters. For example, the following code will give me the object in JSON serialized form
RKObjectMapping *serializationMapping = [[[RKObjectManager sharedManager] mappingProvider] serializationMappingForClass:[self class]];
RKObjectSerializer *ser = [RKObjectSerializer serializerWithObject:self mapping:serializationMapping];
NSObject<RKRequestSerializable> *obj = [ser serializationForMIMEType:RKMIMETypeJSON error:nil];
But the object returned there is intend to be used as a param right away so the following code does not work
[params setValue:[LCSingletonLoggedInUser sharedLoggedInUser].sessionToken forParam:#"token"];
[params setData:obj.HTTPBody forParam:#"data"];
I've also tried various combinations of setObject and setData and obj.HTTPBody as well as just obj right away.
Actually appending obj to params in any way will always result in it adding escape characters which the server can't handle. Setting params = obj will give the correct values to the server but won't include the token.
How about adding it to queryParams?
NSString *resourcePath = [#"/products" stringByAppendingQueryParameters:_queryParams];
Where queryParams is a NSMutableDictionary where you add your token.

Retrieving facebook friendlist and showing it in Tableview

I am using Facebook graph api to retrieve facebook's friend list.
I'm using this code:
-(IBAction)getMeFriendsButtonPressed:(id)sender
{
FbGraphResponse *fb_graph_response = [fbGraph doGraphGet:#"me/friends" withGetVars:nil];
NSLog(#"getMeFriendsButtonPressed: %#", fb_graph_response.htmlResponse);
}
And in return I am getting this at console.
getMeFriendsButtonPressed:
{"data":[{"name":"name1","id":"504181912"},
{"name":"name2","id":"505621879"},
{"name":"name3","id":"520845156"},
{"name":"name4","id":"537539375"}
}
or something like this.
I want to get names, like name1, name2, name3 and show it in table view.
How can I create an array of these name?
Suggestion please
Thanks
This is JSON output. You may refer to the JSON parsing tutorial at this link
http://www.xprogress.com/post-44-how-to-parse-json-files-on-iphone-in-objective-c-into-nsarray-and-nsdictionary/
Hope this works for you.
The respone your are getting is JSON. You should use JSON parser for parsing the JSON String
JSON Framework

How to get array from FBGraph htmlResponse

I want to access array of friends for facebook.so for this i am using FBGraph and i get a string
by this line
NSString *str=(NSString *)fb_graph_response.htmlResponse;
so response is
getMeFriendsButtonPressed: {"data":[{"name":"R.B. Narain","id":"1452788"},{"name":"Jon doe","id":"6564709"}]}
now i wanna get array of friends so for this what can i do.
It is JSON response...... you will need JSON parsing..... For this, either you can write your own parser..... or you can use exising one http://code.google.com/p/json-framework/
thanks.

how to do json parsing in iphone

i am using json parsing in my application
and this is my json data is as follow :
{"response" :
{"success":false,"error":
{"code":7,"description":"You are not logged in"}}}
and i want description means "You are not logged in" i my string
so how can i do that
please help me.....
Check the blog post with sample code and step by step parsing of JSON.
http://www.xprogress.com/post-44-how-to-parse-json-files-on-iphone-in-objective-c-into-nsarray-and-nsdictionary/
http://pessoal.org/blog/2008/12/12/iphone-sdk-parsing-json-data/
http://mobileorchard.com/tutorial-json-over-http-on-the-iphone/
We're using CJSONDeserializer (TouchJSON library) in the iPhone app being developed at work.
Just use the following method:
NSDictionary * dictionary = [[CJSONDeserializer deserializer] deserializeAsDictionary:data error:&error];
where data is of type NSData *. It will convert the JSON string into a dictionary so you could get the value of description as follows:
[[[dictionary objectForKey:#"response"] objectForKey:#"error"] objectForKey:#"description"];