Can't parse JSON sent by SA_OAuthTwitterEngine/MGTwitterEngine - iphone

I'm developing an Twitter app, but in the view where's supposed to be the logged user info, I'm not able to parse the JSON gave by [_engine getUserInformationFor:username];. I'm using SBJSON and tells me this when tries to parse it:
2011-05-07 12:29:45.553 BirdieApp[7122:40b] -JSONValue failed. Error is: Unrecognised leading character at offset 0
This is the code I'm using:
NSLog(#"loggedapi");
NSString *username = [_engine username];
NSString *userinfo = [_engine getUserInformationFor:username];
NSLog(#"user: %#%#", username, userinfo);
NSArray *results = [userinfo JSONValue];
NSDictionary *first = [results objectAtIndex:0];
NSLog(#"screen name = %#", [first objectForKey:#"description"]);
The output that [_engine getUserInformationFor:username]; gaves me is:
2011-05-07 12:29:46.281 BirdieApp[7122:40b] User Info Received: (
{
"contributors_enabled" = false;
"created_at" = "Fri Sep 17 20:40:15 +0000 2010";
"default_profile" = true;
"default_profile_image" = false;
description = "iPhone developer and Photoshop designer. \U30dd\U30fc\U30eb. I developed a web browser, it's called zad0xNET browser, search it in Cydia. Ofcourse this is a fake account!";
"favourites_count" = 0;
"follow_request_sent" = false;
"followers_count" = 4;
following = 0;
"friends_count" = 1;
"geo_enabled" = true;
id = 191964939;
"is_translator" = false;
lang = es;
"listed_count" = 0;
location = "Castilla y Le\U00f3n, Espa\U00f1a";
name = zad0xLOL;
notifications = false;
"profile_background_color" = C0DEED;
"profile_background_image_url" = "http://a3.twimg.com/a/1304019356/images/themes/theme1/bg.png";
"profile_background_tile" = false;
"profile_image_url" = "http://a0.twimg.com/profile_images/1190671091/ProfilePhoto_normal.png";
"profile_link_color" = 0084B4;
"profile_sidebar_border_color" = C0DEED;
"profile_sidebar_fill_color" = DDEEF6;
"profile_text_color" = 333333;
"profile_use_background_image" = true;
protected = 0;
"screen_name" = zad0xs1s;
"show_all_inline_media" = true;
"source_api_request_type" = 12;
status = {
contributors = "";
coordinates = "";
"created_at" = "Sat May 07 07:42:18 +0000 2011";
favorited = false;
geo = "";
id = 66769811828514816;
"in_reply_to_screen_name" = zad0xs1s;
"in_reply_to_status_id" = "";
"in_reply_to_user_id" = 191964939;
place = "";
"retweet_count" = 0;
retweeted = false;
source = "BirdieApp";
"source_api_request_type" = 12;
text = "#zad0xs1s sdjdjdd";
truncated = 0;
};
"statuses_count" = 16;
"time_zone" = Madrid;
url = "http://www.pabloxweb.es";
"utc_offset" = 3600;
verified = false;
}
I think it's JSON, but not sure. I also tried with the url twitter gives but didn't got to work too. The URL is http://api.twitter.com/1/users/show.json?screen_name=zad0xsis , where "zad0xsis" is the twitter username.
Thanks in advance!! ;)

Hii.
Here is a good link OAuth Twitter
This example uses model to get json result in NSDictionary .They called getUsertimeLine method . You can call your own method.

My experience with Cocoa & JSON is
It just won't work right...
or It's not worth all of the trouble to parse it!
You might be better off using the Twitter XML API
https://api.twitter.com/1/users/show.xml?screen_name=zad0xsis
Then using NSXMLParser to parse it (on iPhone): http://ll.io/e17
or
NSXMLDocument (Mac): http://ll.io/ff0
Hope this helps!

Related

How to get data into NSArray from NSDictionary

i am getting following error while getting data from dictionary
"-[__NSCFString objectForKey:]: unrecognized selector sent to instance 0x4fbb80"
here is my code
NSDictionary *dic = [XMLReader dictionaryForXMLString:class.returnData error:nil];
MyGiftsarray = [[dic objectForKey:#"response"] objectForKey:#"gifts"];
for (NSDictionary *element in MyGiftsarray) {
[MyGiftsnamearray addObject:[element objectForKey:#"name"]];
[MyGiftsChkarray addObject:[element objectForKey:#"chk"]];
[MyGiftsIdarray addObject:[element objectForKey:#"id"]];
[MyGiftsApprovedarray addObject:[element objectForKey:#"approved"]];
[MyGiftsisCompletedarray addObject:[element objectForKey:#"completed"]];
}
this is NSDictionary from which i try to get data.
MyGiftsDict->(
{
Appliedpoint = 500;
Requiredpoint = 5000;
approved = N;
chk = 50;
completed = 0;
id = 66;
name = "1-800Flowers.com";
},
{
Appliedpoint = 85;
Requiredpoint = 2500;
approved = N;
chk = 25;
completed = 0;
id = 71;
name = "Bath Body Works";
},
{
Appliedpoint = 5;
Requiredpoint = 2500;
approved = N;
chk = 25;
completed = 0;
id = 75;
name = "Buca di Beppo";
},
{
Appliedpoint = 36;
Requiredpoint = 2500;
approved = N;
chk = 25;
completed = 0;
id = 66;
name = "1-800Flowers.com";
},
{
Appliedpoint = 25;
Requiredpoint = 5000;
approved = N;
chk = 50;
completed = 0;
id = 90;
name = "Jelly Belly";
},
{
Appliedpoint = 120;
Requiredpoint = 500;
approved = N;
chk = 5;
completed = 0;
id = 129;
name = "Amazon.com";
}
)
You are iterating in wrong manner-
Use -
MyGiftsDict = [[dic objectForKey:#"response"] objectForKey:#"gifts"];
And then iterate over dictionary step by step.
You need to use something like -
for(NSString *key in [MyGiftsDict allKeys])
{
[MyGiftsnamearray addObject:[MyGiftsDict objectForKey:key]];
}
Gifts is an NSDictionary in itself. It's not an NSArray! You don't need that for loop. Use simply
[MyGiftsNameArray addObject:[MyGiftsArray objectForKey:#"name"];
et cetera.
I don't think you will need the forcycle.

how to select image from JSON parsed in Array

I am using JSON parsing, I put data in array named JSON Array. In console it looks like
JSON Array=(
{
"created_by" = 42;
"created_on" = "2012-02-29 11:23:37";
"file_description" = "";
"file_is_downloadable" = 0;
"file_is_forSale" = 0;
"file_is_product_image" = 1;
"file_meta" = "";
"file_mimetype" = "image/jpeg";
"file_params" = "";
"file_title" = "hand shovel";
"file_type" = product;
"file_url" = "images/stories/virtuemart/product/cca3cd5db813ee6badf6a3598832f2fc.jpg";
"file_url_thumb" = "images/stories/virtuemart/product/resized/cca3cd5db813ee6badf6a3598832f2fc_90x90.jpg";
"locked_by" = 0;
"locked_on" = "0000-00-00 00:00:00";
"modified_by" = 42;
"modified_on" = "2012-02-29 11:23:37";
ordering = 0;
"product_name" = "Hand Shovel";
published = 0;
shared = 0;
"virtuemart_category_id" = 1;
"virtuemart_media_id" = 13;
"virtuemart_product_id" = 1;
"virtuemart_vendor_id" = 1;
},
{
"created_by" = 42;
"created_on" = "2012-02-29 11:35:09";
"file_description" = "";
"file_is_downloadable" = 0;
"file_is_forSale" = 0;
"file_is_product_image" = 1;
"file_meta" = "";
"file_mimetype" = "image/jpeg";
"file_params" = "";
"file_title" = "our ladder";
"file_type" = product;
"file_url" = "images/stories/virtuemart/product/8cb8d644ef299639b7eab25829d13dbc.jpg";
"file_url_thumb" = "images/stories/virtuemart/product/resized/8cb8d644ef299639b7eab25829d13dbc_90x90.jpg";
"locked_by" = 0;
"locked_on" = "0000-00-00 00:00:00";
"modified_by" = 42;
"modified_on" = "2012-02-29 11:35:09";
ordering = 0;
"product_name" = Ladder;
published = 0;
shared = 0;
"virtuemart_category_id" = 3;
"virtuemart_media_id" = 8;
"virtuemart_product_id" = 2;
"virtuemart_vendor_id" = 1;
},
Now from this i need to select "file_url_thumb" which is actually image in seperate array.
please let me know how can i do this?
//Your array has two dictionary ..
for(NSMutableDictionary *dict in JSON_Array){
NSString *urlString = [dict objectForKey:#"file_url_thumb"];
if(urlString){
NSURL *url = [NSURL urlWithString:urlString];
NSData* imageData = [[NSData alloc] initWithContentsOfURL:url]];
UIImage* image = [[UIImage alloc] initWithData:imageData];
//show your image on imageview
}
}
Hope this will help you
NSString *str_URL =[ [json objectAtIndex:anIndex] valueForKey:#"file_url_thumb"];
NSString *urlString = [[jsonArray objectAtIndex:0] objectForKey:#"file_url_thumb"];
This will give you value for "file_url_thumb" as a string.

how to access all the array of dictionary contents

I have implemented the array of dictionary contents accessing methods but it is access only one content, please help on this
Here is my data file
<__NSArrayM 0x8b7a350>(
{
clkDate = "Wednesday, April 11, 2012";
resData = (
{
data = 10;
independentItem = 0;
module = 6;
newDate = "11-4-2012";
newTime = "13:31";
seqCounter = 101;
sequence = 10007;
session = 101;
timeframe = Breakfast;
timestamp = "2012-04-11 08:01:27 +0000";
title = "Glucose reading : Low";
type = L;
},
{
data = {
hours = 0;
minutes = 0;
which = "Walking:(null)";
};
independentItem = 1;
module = 13;
seqCounter = 101;
sequence = 10009;
session = 101;
timestamp = "2012-04-12 08:01:40 +0000";
title = "Low Cause: Increased Exercise";
}
);
seqCounter = 101;
},
{
clkDate = "Thursday, April 12, 2012";
resData = (
{
data = 200;
independentItem = 0;
module = 6;
newDate = "12-4-2012";
newTime = "13:31";
seqCounter = 102;
sequence = 10017;
session = 101;
timeframe = Breakfast;
timestamp = "2012-04-12 08:01:46 +0000";
title = "Glucose reading : High";
type = H;
},
{
data = {
hours = 0;
minutes = 0;
which = "Other Light:Kkkkk";
};
independentItem = 1;
module = 26;
seqCounter = 102;
sequence = 10022;
session = 101;
timestamp = "2012-04-11 18:30:00 +0000";
title = "High Cause: Decreased Exercise";
}
);
seqCounter = 102;
},
{
clkDate = "Thursday, April 12, 2012";
clkIndependentItem = 1;
resData = (
{
data = {
bathing = 1;
driving = 1;
grocery = 1;
meal = 1;
};
independentItem = 1;
module = 31;
seqCounter = 103;
sequence = 10035;
session = 101;
timestamp = "2012-04-12 08:02:32 +0000";
title = "High Cause: Decreased activity";
}
);
seqCounter = 103;
}
)
In above data file i need to access "independentItem = 1" related items
i tried source code is
for (int i = 0; i < [self.gluClkDetailArray count]; i++)
{
NSMutableDictionary *mDict = [self.gluClkDetailArray objectAtIndex:i];
{
NSMutableArray *mResData = [mDict objectForKey:#"resData"];
NSDate *mTimestamp = [[mResData objectAtIndex:0] objectForKey:#"timestamp"];
NSDateFormatter *mFormatter = [[NSDateFormatter alloc] init];
[mFormatter setDateStyle:NSDateFormatterFullStyle];
NSString *mClkDate = [mFormatter stringFromDate:mTimestamp];
[mDict setValue:mClkDate forKey:#"clkDate"];
NSString *mIndItem = [[mResData objectAtIndex:0] objectForKey:#"independentItem"];
if (mIndItem.intValue == 1)
{
[mDict setValue:mIndItem forKey:#"clkIndependentItem"];
[mIndpendentArray addObject:mDict];
}
[gluClkResultArray addObject:mDict];
}
when ever i put the mResData objectAtIndex:0 instead of i, app got crashed. i have saved array of dictionary values based on the sequence counter
Thanks in advance
Change
NSString *mIndItem = [[mResData objectAtIndex:0] objectForKey:#"independentItem"];
to that:
NSString *mIndItem = [[mResData objectAtIndex:1] objectForKey:#"independentItem"];
The independentItem is in the dictionary that in at index 1, not 0.
Hope it helps

How to fetch the data from JSON?

I am getting response back in JSON format and have never parsed it in the past, so could somebody please help me understand this.
Here's the Json response i am getting:
Twitter response:
(
{
contributors = "<null>";
coordinates = "<null>";
"created_at" = "Tue Nov 29 15:48:35 +0000 2011";
entities = {
hashtags = (
);
media = (
{
"display_url" = "pic.twitter.com/650E1WRY";
"expanded_url" = "http://twitter.com/ashu1702/status/141544088850796545/photo/1";
id = 141544088854990848;
"id_str" = 141544088854990848;
indices = (
22,
42
);
"media_url" = "http://p.twimg.com/AfbdmVBCEAAPJvT.jpg";
"media_url_https" = "https://p.twimg.com/AfbdmVBCEAAPJvT.jpg";
sizes = {
large = {
h = 279;
resize = fit;
w = 215;
};
medium = {
h = 279;
resize = fit;
w = 215;
};
small = {
h = 279;
resize = fit;
w = 215;
};
thumb = {
h = 150;
resize = crop;
w = 150;
};
};
type = photo;
url = "http://t.co/650E1WRY";
}
);
urls = (
);
"user_mentions" = (
);
};
favorited = 0;
geo = "<null>";
id = 141544088850796545;
"id_str" = 141544088850796545;
"in_reply_to_screen_name" = "<null>";
"in_reply_to_status_id" = "<null>";
"in_reply_to_status_id_str" = "<null>";
"in_reply_to_user_id" = "<null>";
"in_reply_to_user_id_str" = "<null>";
place = "<null>";
"possibly_sensitive" = 0;
"retweet_count" = 0;
retweeted = 0;
source = "iOS";
text = "I am in Syria #(null) http://t.co/650E1WRY";
truncated = 0;
user = {
"contributors_enabled" = 0;
"created_at" = "Sun May 01 15:20:52 +0000 2011";
"default_profile" = 1;
"default_profile_image" = 1;
description = "<null>";
"favourites_count" = 0;
"follow_request_sent" = "<null>";
"followers_count" = 0;
following = "<null>";
"friends_count" = 5;
"geo_enabled" = 0;
id = 291164338;
"id_str" = 291164338;
"is_translator" = 0;
lang = en;
"listed_count" = 0;
location = "<null>";
name = "Ashutosh Tiwari";
notifications = "<null>";
"profile_background_color" = C0DEED;
"profile_background_image_url" = "http://a0.twimg.com/images/themes/theme1/bg.png";
"profile_background_image_url_https" = "https://si0.twimg.com/images/themes/theme1/bg.png";
"profile_background_tile" = 0;
"profile_image_url" = "http://a2.twimg.com/sticky/default_profile_images/default_profile_3_normal.png";
"profile_image_url_https" = "https://si0.twimg.com/sticky/default_profile_images/default_profile_3_normal.png";
"profile_link_color" = 0084B4;
"profile_sidebar_border_color" = C0DEED;
"profile_sidebar_fill_color" = DDEEF6;
"profile_text_color" = 333333;
"profile_use_background_image" = 1;
protected = 0;
"screen_name" = ashu1702;
"show_all_inline_media" = 0;
"statuses_count" = 62;
"time_zone" = "<null>";
url = "<null>";
"utc_offset" = "<null>";
verified = 0;
};
}
)
Specifically i just need the "expanded_url" and i am doing this in my code to get it:
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&error];// where response data is coming from the call made to twitter timeline_api
NSString * callBackURL = [dict objectForKey:#"expanded_url"];
On doing this i am getting this error:
2011-11-29 09:50:31.214 L'Occitane[1119:19d07] -[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0x85a50d0
2011-11-29 09:50:31.215 L'Occitane[1119:19d07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0x85a50d0'
PLease help!!!
Thanks,
When converted to Cocoa Foundation objects, JSON decomposes into a bunch of nested NSArrays and NSDictionarys ultimately leading to NSStrings, NSNumbers and NSNulls. Ideally you'd work forwards from the web service's documentation but you can work backwards from the description that the root object prints through NSLog.
From your log I can see that expanded_url is a member of an unnamed object which is the first entry in the media array that is a member of the entities object. The unnamed object containing entities is part of an array, which is what the JSON deserialiser will return. So you want to do something like (if written out in long form for exposition, and without any validation):
NSArray *result = [NSJSONSerialization ...
NSDictionary *firstObject = [result objectAtIndex:0];
NSDictionary *entities = [firstObject objectForKey:#"entities"];
NSArray *media = [entities objectForKey:#"media"];
NSDictionary *relevantObject = [media objectAtIndex:0];
NSString *expandedUrl = [relevantObject objectForKey:#"expanded_url"];
The main risk in there is that the web service may return an empty array for media, causing objectAtIndex:0 to raise an exception, or that it will return something other than an array as the root object or in some other way adjust a type somewhere along the line. Objective-C's usual rules about messaging nil should save you from most problems with the dictionaries. Quite how much validation to apply is a judgment call.

iPhone: how to work with this NSArray

in my NSArray contains below info like userName, screenName, Description etc
this information i have to *display on simulator r device with respectively UIElements *
whether i have to keep the NSArray in NSDictionary if so how i have to do that can you tell me how to work with this
i am using this delegate and getting below info on console
-(void)userInfoReceived:(NSArray *)userInfo forRequest:(NSString *)connectionIdentifier {
NSLog(#"User Info Received: %#", userInfo);
}
this is info in Debug Console
{
"created_at" = "Fri Nov 19 17:22:47 +0000 2010";
description = "";
"favourites_count" = 0;
"followers_count" = 99;
following = 0;
"friends_count" = 860;
lang = en;
"listed_count" = 0;
location = "";
name = "preeti singh";
"profile_background_image_url" = "http://a3.twimg.com/a/1292606737/images/themes/theme1/bg.png";
"profile_image_url" = "http://a2.twimg.com/a/1292531900/images/default_profile_6_normal.png";
"profile_use_background_image" = true;
"screen_name" = preetisingjik;
}
Your signature suggest that a NSArray should be passed in while obviously, a NSDictionary is instead !
I suggest you change the protocol to
(void)userInfoReceived:(NSArray *)userInfo forRequest:(NSString *)connectionIdentifier
Then access the value from your dictionary using
NSString *desc = [userInfo objectForKey:#"description"];