Parse json with NSJSONSerialization class using objectForKey in iOS - iphone

I am new in iOS development. I use this code to connect to my REST Web Service and fetch data in Json format.
NSString *url=#"URL_Address";
NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
NSURLResponse *resp = nil;
NSError *err = nil;
NSData *response = [NSURLConnection sendSynchronousRequest: theRequest returningResponse: &resp error: &err];
// NSString * theString = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
// NSLog(#"response: %#", theString);
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData: response options: NSJSONReadingMutableContainers error: &err];
if (!jsonArray) {
NSLog(#"Error parsing JSON: %#", err);
} else {
for(NSDictionary *item in jsonArray) {
NSLog(#" %#", item);
NSLog(#"---------------------------------");
}
}
Now I want to seperate them via objectForKey. I used this code inside the loop :
NSString *name = [item objectForKey:#"name"];
It does not work. I got this error:
2012-07-31 12:48:38.426 LearningAFNetworking[390:f803] -[__NSArrayM objectForKey:]: unrecognized selector sent to instance 0x6844460
2012-07-31 12:48:38.428 LearningAFNetworking[390:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM objectForKey:]: unrecognized selector sent to instance 0x6844460'
*** First throw call stack:
(0x13c8022 0x1559cd6 0x13c9cbd 0x132eed0 0x132ecb2 0x2f40 0x2bf8 0xd9a1e 0x38401 0x38670 0x38836 0x3f72a 0x290b 0x10386 0x11274 0x20183 0x20c38 0x14634 0x12b2ef5 0x139c195 0x1300ff2 0x12ff8da 0x12fed84 0x12fec9b 0x10c65 0x12626 0x254d 0x24b5 0x1)
terminate called throwing an exception(lldb)
can you help me?

You have to access the 0 item of item array:
NSArray *mainArray = [item objectAtIndex:0];
(NSDictionary *obj in mainArray) {
NSString *name = [obj objectForKey:#"name"];
}
When you got this error: -[__NSArrayM objectForKey:], you need to realize that the object that you're trying to access isn't a dictionary. It's an array (__NSArrayM), so you have to first access the 0 index, and then starting exploring the dictionary.
Take a look at this amazing tutorial, from Ray Wenderlich, that explains everything about crashes.

As Alberto said, you assumed that item is an NSDictionary while in reality is an NSArray containing multiple dictionaries. Now for the drill-down part you can use valueForKeyPath as shown below. So your for loop should be something like this:
NSArray *items = [jsonArray objectAtIndex:0];
for (NSDictionary *item in items){
NSString *name = [item valueForKey:#"name"];
// You can also get nested properties like this
NSString *projectName = [item valueForKeyPath:#"project.name"];
}
Another useful thing to know is that if you'd like to get all 'name' values for example, you could use valueForKey on your items array, which would automatically call valueForKey on each dictionary for you. Example:
NSArray *items = [jsonArray objectAtIndex:0];
NSArray *names = [items valueForkey:#"name"];

From the error and from the log output of your code it looks like your are trying to execute the selector objectForKeyon a NSArray.
Try to change NSString *name = [item objectForKey:#"name"];to NSString *name = [[item objectAtIndex: 0]objectForKey:#"name"];

May this code help you to get the first object of type dictionary from the NSArray
[[jsonArray objectAtIndex:0]objectForKey:#"object-key"];

-(void)clientServerCommunication
{
NSURL *url = [NSURL URLWithString:#"http://182.72.122.106/iphonetest/getTheData.php"];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:req delegate:self];
if (connection)
{
webData = [[NSMutableData alloc]init];
}
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[webData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[webData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSDictionary *responseDict = [NSJSONSerialization JSONObjectWithData:webData options:0 error:nil];
/*Third party API
NSString *respStr = [[NSString alloc]initWithData:webData encoding:NSUTF8StringEncoding];
SBJsonParser *objSBJson = [[SBJsonParser alloc]init];
NSDictionary *responseDict = [objSBJson objectWithString:respStr]; */
resultArray = [[NSArray alloc]initWithArray:[responseDict valueForKey:#"result"]];
NSLog(#"resultArray: %#",resultArray);
[self.tableView reloadData];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
custcell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(custcell==nil)
{
custcell=[[[NSBundle mainBundle]loadNibNamed:#"custumCell" owner:self options:nil]objectAtIndex:0];
}
custcell.persnName.text=[[arr objectAtIndex:indexPath.row]valueForKey:#"Name"];
//else if (objsegment.selectedSegmentIndex==1)
//{
custcell.persnAge.text=[[arr objectAtIndex:indexPath.row]valueForKey:#"Age"];
[sortcontrol addTarget:self action:#selector(SegmentbtnCLK:) forControlEvents:UIControlEventValueChanged];
//objsegment.selectedSegmentIndex=0;
// }
return custcell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here, for example:
//Create the next view controller.
detailViewController *detailViewController1 = [[detailViewController alloc]initWithNibName:#"detailViewController" bundle:nil];
//detailViewController *detailViewController = [[detailViewController alloc] initWithNibName:#"detailViewController" bundle:nil];
// Pass the selected object to the new view controller.
// Push the view controller.
detailViewController1.nextDict = [[NSDictionary alloc]initWithDictionary:[resultArray objectAtIndex:indexPath.row]];
[self.navigationController pushViewController:detailViewController1 animated:YES];
// Pass the selected object to the new view controller.
// Push the view controller.
// [self.navigationController pushViewController:detailViewController animated:YES];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
empName.text=[nextDict valueForKey:#"name"];
deptlbl.text=[nextDict valueForKey:#"department"];
designationLbl.text=[nextDict valueForKey:#"designation"];
idLbl.text=[nextDict valueForKey:#"id"];
salaryLbl.text=[nextDict valueForKey:#"salary"];
NSString *ImageURL = [nextDict valueForKey:#"image"];
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:ImageURL]];
image.image = [UIImage imageWithData:imageData];
}

Related

How to display array of JSON values in custom tableView cells

I want to pass values inside for(NSDictionary *jsonDictionary in myJsonArray) which I get in NSLog to [array addObject:[[SaveList alloc] initWithEmail:email withPhone:phone withDate:date withName:name]];
Code is here
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
NSError *error = nil;
NSURL *url = [NSURL URLWithString:#" http:// Some url "];
NSString *json = [NSString stringWithContentsOfURL:url encoding:NSASCIIStringEncoding error:&error];
NSLog(#"\n\n JSON : %#, \n Error: %#", json, error);
if(!error)
{
NSData *jsonData = [json dataUsingEncoding:NSASCIIStringEncoding];
NSArray *myJsonArray = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil];
// NSDictionary *jsonDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&error];
for(NSDictionary *jsonDictionary in myJsonArray)
{
NSLog(#"JSON Dictionary = %#", jsonDictionary);
NSString *name = jsonDictionary[#"Name"];
NSString *date = jsonDictionary[#"Date"];
NSString *email = jsonDictionary[#"Email"];
NSString *phone = jsonDictionary[#"Phone"];
NSLog(#"Name = %#", name);
NSLog(#"Date = %#", date);
NSLog(#"Email = %#", email);
NSLog(#"Phone = %#", phone);
}
}
});
//Table implementation
array = [[NSMutableArray alloc]init];
//**Get email, phone, date, name here**
[array addObject:[[SaveList alloc] initWithEmail:email withPhone:phone withDate:date withName:name]];
self.tableView.dataSource = self;
self.tableView.delegate = self;
Why don't you add the objects as you receive them? Since this block of code will be executed asynchronously you could prepare your array, set your tableview and then execute the block where you fill your array and refresh the tableview.
Something like this:
// Prepare your array
array = [NSMutableArray arrayWithCapacity:0];
// Set your tableview's datasource & delegate
self.tableView.dataSource = self;
self.tableView.delegate = self;
// Fetch data asynchronously
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
NSError *error = nil;
NSURL *url = [NSURL URLWithString:#" http:// Some url "];
NSString *json = [NSString stringWithContentsOfURL:url encoding:NSASCIIStringEncoding error:&error];
NSLog(#"\n\n JSON : %#, \n Error: %#", json, error);
if(!error)
{
NSData *jsonData = [json dataUsingEncoding:NSASCIIStringEncoding];
NSArray *myJsonArray = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil];
NSMutableArray *tmp = [NSMutableArray arrayWithCapacity:myJsonArray.count];
for(NSDictionary *jsonDictionary in myJsonArray)
{
NSString *name = jsonDictionary[#"Name"];
NSString *date = jsonDictionary[#"Date"];
NSString *email = jsonDictionary[#"Email"];
NSString *phone = jsonDictionary[#"Phone"];
//**Get email, phone, date, name here**
[tmp addObject:[[SaveList alloc] initWithEmail:email
withPhone:phone
withDate:date
withName:name]];
}
// Reload your tableview
dispatch_sync(dispatch_get_main_queue(), ^{
array = tmp; // Or add them to your datasource array, whatever suits you...
[self.tableView reloadData];
});
}
});
set number of rows
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
[array count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
SavedList *savedList = [array objectAtIndex:indexPath.row];
cell.text = savedList.name;
}
return cell;
}
I hope it will be helpful.
// U need to reload table view data whenever you add new object in it. It does work like a thread.
dispatch_sync(dispatch_get_main_queue(), ^{
array = myJsonArray;
[self.tableView reloadData];

'NSInvalidArgumentException' testViewController tableView:numberOfRowsInSection

I trying to bind the Json parse data with table view using below code and getting exception. I am using ASIHttpRequest and Json as well.
**Error details:**
webservice[2864:f803]
***
Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: '-[testViewController tableView:numberOfRowsInSection:]:
unrecognized selector sent to instance 0x6ecc4e0'
***
NSString *responseString = [request responseString];
SBJsonParser *parser=[[SBJsonParser alloc]init];
NSDictionary *obj=[[NSDictionary alloc]init];
obj=[parser objectWithString:responseString error:nil];
return obj.count;
NSString *responseString = [request responseString];
SBJsonParser *parser=[[SBJsonParser alloc]init];
NSDictionary *obj=[[NSDictionary alloc]init];
obj=[parser objectWithString:responseString error:nil];
NSMutableArray *statuses = [[NSMutableArray alloc]init];
for (NSDictionary *status in obj)
{
[statuses addObject:status];
}
self.listdata=statuses;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: #"myCell"];
if (cell == nil) {
cell = [[ UITableViewCell alloc ] initWithFrame:CGRectZero];
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [listdata objectAtIndex:row];
return cell;
Looks like your table view data source is not implementing the method tableView:numberOfRowsInSection:

iphone load table with wcf

atm i can successfully loading data from a WFC, read the json and put it on the right objects.
But my problem comes when i need to show a table with this data, cuz i don't know where to play the method or when should i call it. Atm looks like the table is created and after that i get the data from the web. Should i reload the table or can i get the info before the class calls cellForRowAtIndexPath: ?
Is there a way to make a connection synchronic and not synchronic? because in this case, if i cant get the list of eventos form wfc its has not point showing a table. So
Thx in advance!
my code:
-(id)init{
//call superclass designated inizialzer
self= [super initWithStyle:UITableViewStyleGrouped];
if(self){
[[self navigationItem] setTitle:#"Eventos"];
responseData = [[NSMutableData data] retain];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:#"http://xxx.xxx.xxx.xxx/..."]];
[[[NSURLConnection alloc] initWithRequest:request delegate:self]autorelease];
}
return self;
}
about connection:
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[responseData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[responseData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
// label.text = [NSString stringWithFormat:#"Connection failed: %#", [error description]];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
[responseData release];
NSError *error;
SBJSON *json = [[SBJSON new] autorelease];
NSDictionary *luckyNumbers = [json objectWithString:responseString error:&error];
[responseString release];
if (luckyNumbers == nil)
// label.text = [NSString stringWithFormat:#"JSON parsing failed: %#", [error localizedDescription]];
[luckyNumbers release];
else {
for (NSDictionary *object in [luckyNumbers objectForKey:#"EResult"]) {
Evento *e=[[Evento alloc] init];
e.nombre= [object objectForKey:#"nombre"];
e._id= (int)[object objectForKey:#"id"];
e.fecha= [object objectForKey:#"fecha"];
[[EventoStore defaultStore]addEvento:e];
[e release];
}
}
}
about the table it self:
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [[[EventoStore defaultStore] allEventos]count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//check for reusable cell first and use it
UITableViewCell *cell= [tableView dequeueReusableCellWithIdentifier:#"UITableViewCell"];
//if there is no reusable cell, we create one
if(!cell){
cell= [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1
reuseIdentifier:#"UITableViewCell"]autorelease];
}
Evento *e=[[[EventoStore defaultStore] allEventos] objectAtIndex:[indexPath row]];
[[cell textLabel] setText:[e nombre]];
return cell;
}
-(void)tableView:(UITableView *) aTableView didSelectRowAtIndexPath:(NSIndexPath *) indexPax{
LotesViewController *loteViewController= [[[LotesViewController alloc]init]autorelease];
NSArray *eventos=[[EventoStore defaultStore]allEventos];
[loteViewController setEvento: [eventos objectAtIndex:[indexPax row]]];
[[self navigationController]pushViewController:loteViewController animated:YES];
}
you should reload the table after getting the data. you can show the activity indicator on the table till you get the data and once you get the data, you can remove the activity indicator and reload the table. this way you can find the solution to your problem. No need to go for synchronous connection. Just add the activity indicator once the connection is satrted and remove it when data comes.

Json Parser output display in tableview

I am trying to parse using JSON Parser and the result which i get i have to put it into table view. I've passed a constant key value and a string .
Is there parsing steps wrong? or missed.
I have included the code for the JSON parser.
Thanks in advance.
SBJSON *parser = [[SBJSON alloc] init];
NSString *urlString =[NSString stringWithFormat:#"http://api.shopwiki.com/api/search?key=%#&q=%#",apiKey, string];
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL: url];
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
NSMutableArray *statuses = [[NSMutableArray alloc]init];
statuses = [parser objectWithString:json_string error:nil];
NSLog(#"Array Contents: %#", statuses);
NSMutableArray *statuses0 = [[NSMutableArray alloc]init];
statuses0 = [statuses valueForKey:#"offers"];
NSLog(#"Array Contents: %#", statuses0);
//For Title
NSMutableArray *statuses1 = [[NSMutableArray alloc]init];
statuses1 = [[[statuses valueForKey:#"offers"] valueForKey:#"offer"]valueForKey:#"title"];
NSLog(#"Array Contents 4 Title: %#", statuses1);
Here in statuses1 array i get 20 objects which are all titles, now i just want to display that titles into tableview:-
snippet code for tableview:-
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSLog(#"status count:%d",[statuses1 count]);
return [statuses1 count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"Inside Tableview");
int counter=indexPath.row;
NSString *CellIdentifier = [NSString stringWithFormat:#"%d",counter];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
}
NSLog(#"Inside Tableview 1");
cell.textLabel.text=[statuses1 objectAtIndex:indexPath.row];
NSLog(#"Inside Tableview 2");
return cell;
}
I get Bad excess exception whten it hits on :-
cell.textLabel.text=[statuses1 objectAtIndex:indexPath.row];
Please give me the solution
thanks in advance:-
If you're getting EXC_BAD_ACCESS on that line, it's probably because statuses1 has been released by the time cellForRowAtIndexPath is called. What block of code is this line in?
NSMutableArray *statuses1 = [[NSMutableArray alloc]init];
The statuses1 variable above is local to whatever scope you've declared it in. Do you then assign it to your UITableViewController.statuses1 ivar? Is that a retained property?

iPhone JSON Object

I'm trying to create a application that will retrieve JSON data from an HTTP request, send it to a the application main controller as a JSON object then from there do further processing with it.
Where I'm stick is actually creating a class that will serve as a JSON class in which will take a URL, grab the data, and return that object.
Alone, im able to make this class work, however I can not get the class to store the object for my main controller to retrieve it.
Because im fairly new to Objective-C itself, my thoughts are that im messing up within my init call:
-initWithURL:(NSString *) value
{
responseData = [[NSMutableData data] retain];
NSURL *theURL = [NSURL URLWithString:value];
NSURLRequest *request = [NSURLRequest requestWithURL:theURL];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
return self;
}
The processing of the JSON object takes place here:
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[connection release];
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
[responseData release];
NSError *jsonError;
SBJSON *json = [[SBJSON new] autorelease];
NSDictionary *parsedJSON = [json objectWithString:responseString error:&jsonError];
// NSArray object.
listings = [parsedJSON objectForKey:#"posts"];
NSEnumerator *enumerator = [listings objectEnumerator];
NSDictionary* item;
// to prove that it does work.
while (item = (NSDictionary*)[enumerator nextObject]) {
NSLog(#"posts:id = %#", [item objectForKey:#"id"]);
NSLog(#"posts:address = %#", [item objectForKey:#"address"]);
NSLog(#"posts:lat = %#", [item objectForKey:#"lat"]);
NSLog(#"posts:lng = %#", [item objectForKey:#"lng"]);
}
[responseString release];
}
Now when calling the object within the main controller I have this bit of code in the viewDidLoad method call:
- (void)viewDidLoad {
[super viewDidLoad];
JSON_model *jsonObj = [[JSON_model alloc] initWithURL:#"http://localhost/json/faith_json.php?user=1&format=json"];
NSEnumerator *enumerator = [[jsonObj listings] objectEnumerator];
NSDictionary* item;
//
while (item = (NSDictionary*)[enumerator nextObject]) {
NSLog(#"posts:id = %#", [item objectForKey:#"id"]);
NSLog(#"posts:address = %#", [item objectForKey:#"address"]);
NSLog(#"posts:lat = %#", [item objectForKey:#"lat"]);
NSLog(#"posts:lng = %#", [item objectForKey:#"lng"]);
}
}
take a look at TouchJSON project - http://code.google.com/p/touchcode/wiki/TouchJSON
I think things are happening in the wrong order.
enter viewDidLoad
[JSON_Model initWithURL] is called from viewDidLoad
initWithURL starts fetching data asynchronously.
initWithURL finishes and returns to viewDidLoad.
viewDidLoad continues and displays the empty content of listings
... Time passes whilst the server generates the JSON and returns it.
connectionDidFinishLoading is called once the iPhone recieves the data
connectionDidFinishLoading populates listings with the JSON data.
listings is not access again
the view is never told to refresh once the JSON data has been loaded and parsed.
I found this website to be helpful for using a JSON object in my app.