Parsing JSON (?) response from Facebook SDK iOS - ios

I'm tearing my hair out. I'm using the Facebook SDK in an iOS application to request the score of a player. The response I get looks like this when output to the console:
{
data = (
{
application = {
id = 790595290993938;
name = "Laser Blast";
};
score = 100;
user = {
id = 10152606174287397;
name = "Chris Koncewicz";
};
}
);
}
How do I get the value for the score? I've tried making an NSDictionary using:
NSDictionary *d = [NSJSONSerialization JSONObjectWithData:result options:kNilOptions error:&error];
However, I can't see how to access the score value.
Any help greatly appreciated.
EDIT:
This is the code snippet that runs the request and where I'm trying to work with the response:
[getScore startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
NSLog(#"%#", result);
NSDictionary *d = [NSJSONSerialization JSONObjectWithData:result options:kNilOptions error:&error];
}];

You might overlooked the fact numbers are stored as NSNumber objects in collections:
NSInteger score = facebookJson[#"data"][0][#"score"].integerValue;

Related

NSCFString 0x2749a0 valueForUndefinedKey this class is not key value coding-compliant for the key push_type

My Json
{
aps = {
alert = {
body = "This is a testing message !!!";
title = Notification;
};
};
data = "{\"push_type\":6,\"badge\":1,\"alert\":\"This is a testing message !!!\",\"sound\":\"default\",\"content-available\":\"1\",\"is_login\":2}";
"gcm.message_id" = "0:1494478903994917%6c350cc06c350cc0"; }
I want to get push_type like here it is 6.
My code is:
NSDictionary *data=[[userInfo valueForKey:#"data"] valueForKey:#"push_type"]];
After run this line an error occur push_type is not key value coding complaints...
Could you please help me where i am wrong,or how can i achieve this,please refer me an example,link thanks!
Try with it.
NSString *jsonString=[[userInfo valueForKey:#"data"];
NSData *JsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *data = [NSJSONSerialization JSONObjectWithData:JsonData options:0 error:nil];
Now from "data" dictionary you can get any value from its key, like "push_type" etc...

How To Get Particular Values From Json Response Using Objective C?

I am trying to get response of first key value without mentioning key name of "A" using objective C. I cant get exactly, please help me to get from below response.
NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves | NSJSONReadingMutableContainers error:&error];
NSDictionary *response = [JSON[#"response"]firstObject];
response = {
A = {
company = (
{
no = "115";
student = "Mich";
school = (
{
grade = A;
}
);
test = "<null>";
office = tx;
}
);
};
}
There are a few ways to do this, depending on your exact requirements. If you just need to access the value of each key in JSON[#"response"], you can enumerate the JSON dictionary:
[JSON enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
NSDictionary *dict = (NSDictionary *)obj;
...
if (shouldStop) { // whatever condition you want, if any
*stop = YES;
}
}];
If you want some kind of ordering, you need to use [JSON allKeys]:
NSArray *keys = [[JSON allKeys] sortedArrayUsing...]; // Use whichever sort method you like
for (NSString *key in keys) {
NSDictionary *dict = JSON[key];
...
}
If all you want are the values, you can use [JSON allValues]. Sort if desired.

Can't access event name from Facebook graph api response iOS

I am calling the graph api to retrieve my user's events.And I get back an NSDictionary response. My problem is that I'm trying to access the names of the events but can't figure out the correct way of doing it. Can you please help me with this?
My response :
events = {
data = (
{
id = 16245704637388667;
name = "My event name";
place = {
id = 278379712223737;
location = {
city = Beijing;
country = China;
latitude = "53.598408783333";
....
Mycode to retrieve the vent name:
if ([result objectForKey:#"data"]){
NSArray *events = [result objectForKey:#"data"];
NSString *text=#"You don't have events!";
for (NSDictionary* myevent in events) {
NSString *myeventName = [myevent objectForKey:#"name"];
NSLog(#"%#",myeventName);
}
}
Found a solution:
[[[FBSDKGraphRequest alloc] initWithGraphPath:#"me/events" parameters: #{#"fields": #"name"}]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error) {
NSMutableArray* events = [result objectForKey:#"data"];
events names = [events valueForKey:#"name"];
}
}];`

parse JSON weather object from Open Weather Map API using AFNetworking

I am using AFNetworking to retrieve information about the weather for a specific location, e.g:
http://api.openweathermap.org/data/2.5/weather?q={New%20York%20City}
I am using the AFNetworking framework but I am having the problems parsing some objects of the JSON.
If I have an NSDictionary with the MAIN object information from the JSON:
NSDictionay *main = [responseObject objectForKey:#"main"];
If I log the main NSDictionary I will get the following valid output:
"main":{
"temp":296.78;
"pressure":1011;
"humidity":69;
"temp_min":293.15;
"temp_max":299.82
};
Although if I create a NSDictionary containing the weather object I will get the following information whenever logging it to the console:
NSDictionay *weather = [responseObject objectForKey:#"weather"];
"weather":(
{
"id":801;
"main":"Clouds";
"description":"few clouds";
"icon":"02d"
}
);
The parsed information contains ( brackets instead of [ from the original response. This does not allow me to correctly access the inside attributes of the weather object.
Summing up, I am able to access all the inside variables of the MAIN object, but I cannot access the attributes of the Weather object (e.g access the icon attribute).
Can someone help me with this ?
Thank you,
You calling your service as below.
NSString *query = #"http://api.openweathermap.org/data/2.5/weather?q={New%20York%20City}";
NSLog(#"%#",query);
query = [query stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSData *jsonData = [[NSString stringWithContentsOfURL:[NSURL URLWithString:query] encoding:NSUTF8StringEncoding error:nil] dataUsingEncoding:NSUTF8StringEncoding];
NSError *error = nil;
NSDictionary *results = jsonData ? [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error] : nil;
Now print Response :
NSLog(#"weather==%#",[results objectForKey:#"weather"]);
NSLog(#"description==%#",[[[results objectForKey:#"weather"] objectAtIndex:0] objectForKey:#"description"]);
NSLog(#"icon==%#",[[[results objectForKey:#"weather"] objectAtIndex:0] objectForKey:#"icon"]);
NSLog(#"id==%#",[[[results objectForKey:#"weather"] objectAtIndex:0] objectForKey:#"id"]);
NSLog(#"main==%#",[[[results objectForKey:#"weather"] objectAtIndex:0] objectForKey:#"main"]);
Your Response is :
whwather==(
{
description = "sky is clear";
icon = 01d;
id= 800;
main= Clear;
}
)
description== "sky is clear";
icon == 01d;
id == 800;
main == Clear;

using json, read contents in objective c

I'm learning a very basic method to download data from a weather api.
Basically trying to follow a tutorial.
Using the URL, I am able to download the data in JSON format into a dictionary. Then put into an array.
My question now is how do I read the particular value of an item in the array.
For example, when I do an NSLOG of the array I get the following... I only cut/paste a couple as there are 55 items.
So my question is how do I grab a particular value our of this array?
2013-03-18 14:37:57.576 LocalWeatherV3[1220:c07] loans: {
UV = 2;
"dewpoint_c" = "-4";
"dewpoint_f" = 24;
"dewpoint_string" = "24 F (-4 C)";
"display_location" = {
city = "Jersey City";
country = US;
"country_iso3166" = US;
elevation = "47.00000000";
full = "Jersey City, NJ";
latitude = "40.75180435";
longitude = "-74.05393982";
state = NJ;
"state_name" = "New Jersey";
zip = 07097;
};
estimated = {
};
"feelslike_c" = 2;
"feelslike_f" = 35;
"feelslike_string" = "35 F (2 C)";
"forecast_url" = "http://www.wunderground.com/US/NJ/
here is a piece of the .m
- (void)fetchedData:(NSData *)responseData {
//parse out the json data
NSError* error;
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:responseData //1
options:kNilOptions
error:&error];
NSArray* latestLoans = [json objectForKey:#"current_observation"]; //2
NSLog(#"loans: %#", latestLoans); //3
// 1) Get the latest loan
//NSDictionary* loan = [latestLoans objectAtIndex:1];
NSInteger counter = [latestLoans count];
thanks in advance!!
so when I do this
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:responseData //1
options:kNilOptions
error:&error];
and i mouse over the local watch, I see
json NSDictionary * 0x08d62d40
[0] key/value pair
key id 0x08d61cf0
value id 0x08d62100
[1] key/value pair
key id 0x08d62150
value id 0x08d633a0
then i do
NSArray* latestLoans = [json objectForKey:#"current_observation"]; //2
NSLog(#"loans: %#", latestLoans); //3
and one of the items I want is in "latestloans" which is where all that data shows up. so I cant figure out how to grab one of the items
Let's assume you're trying to grab the forecast url. It's as simple as:
// update this line
NSDictionary *latestLoans = [json objectForKey:#"current_observation"];
// url variable will contain the first forecast url in the array
NSString *url = [latestLoans objectForKey:#"forecast_url"];

Resources