collapse expand header section using json file - iphone

In fact, I am new in iOS programming, and I am trying to learn it by myself.
I have a sectioned table view, with header sections, but I want the view to be able to collapse expand the rows in section when this section is tapped.
I use this tutorial: http://blog.paxcel.net/blog/expandablecollapsible-table-for-ios/
But In my application, I have a Json File and not a Plist file.
So I use in setCategoryArray() function:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData* data = [NSData dataWithContentsOfURL:
[NSURL URLWithString: #"http://......./catjsonf.php"]];
NSError* error;
json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
jsonResults = [json objectForKey:#"nodes"];
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
});
instead of :
NSURL *url = [[NSBundle mainBundle] URLForResource:#”CategoryList” withExtension:#”plist”];
NSArray *mainArray = [[NSArray alloc] initWithContentsOfURL:url];
But it doesn't work - should I do something else?

check out my blog it will surely help you. here is the link.
Expandable/Collapsible Table For iOS
you need to do the following things
First design your classes according to your json file as in my case it is "Category".
Once you are done with model classes you need to edit the following method to prepare array of model objects.
Hint: From json, we can directly find out our dictionary like
id result = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
where result can further casted to get the dictinary object. And the load data simply like the following method
- (void) setCategoryArray
{
NSURL *url = [[NSBundle mainBundle] URLForResource:#”CategoryList” withExtension:#”plist”];
NSArray *mainArray = [[NSArray alloc] initWithContentsOfURL:url];
NSMutableArray *categoryArray = [[NSMutableArray alloc] initWithCapacity:[mainArray count]];
for (NSDictionary *dictionary in mainArray)
{
Category *category = [[Category alloc] init];
category.name = [dictionary objectForKey:#"name"];
category.list = [dictionary objectForKey:#"list"];
[categoryArray addObject:category];
}
self.categoryList = categoryArray;
}
Once this done, the only thing left to edit the table view's data source and delegate methods accordingly.

Related

Break String that came from Json

I have a String that I got from a webserver which came in json format, but the string is huge with everything in it. I tried using the NSDICTIONARY but to no success. I was wondering what would be the best approach to break this string and add to different strings and eventually put it all in a class of strings. Thanks for the help! Here is my code:
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]init];
[request setURL:[NSURL URLWithString:#"http://mym2webdesign.com/meiplay/paulsuckedabuffalo/artists.php"]];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; //Or async request
returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSError *error=nil;
NSLog(#"HHHHHHHHHHHHHH"); //use this to know how far Im getting
NSLog(returnString); // Look at the console and you can see what the restults are
/*NSDictionary *results = [returnString JSONValue];
NSString *ID = [results objectForKey:#"ID"]; // for example
NSLog(#"ID Number: %#", ID);*/
Here is some of the log i get:
[{"ID":"1","name":"kevin","bio":"kevins bio"},{"ID":"1","name":"kevin","age":"20"},{"ID":"2","name":"Cesar","bio":"Cesar bio"},{"ID":"2","name":"Cesar","age":"19"},{"ID":"3", "name":"Katherine", "bio":"Katherines bio"},{"ID":"3", "name":"Katherine", "age":"22"}]
You are doing it wrong. Its a NSArray of NSDictionaries. So first you need to assign it to NSArray and then loop over it to get each individual NSDictionary. See below.
NSArray *results = [returnString JSONValue];
for(NSDictionary *record in results)
{
NSLog(#"ID: %#", [record objectForKey:#"ID"]);
}
You'll probably be better off just using NSJSONSerialization if your app is targeted for at or over iOS 5.0:
NSArray *JSONArray = [NSJSONSerialization JSONObjectWithData:returnData options:0 error:&error];
You might need to experiment with using NSArray vs. NSDictionary, etc., but this should be an overall simpler solution.
Try this :
NSArray *results = [returnString JSONValue];
for (int i=0; i<[results count];i++) {
NSDictionary *DetailDictonary=[results objectAtIndex:i];
NSString *strid=[DetailDictonary objectForKey:#"ID"];
NSString *strName=[DetailDictonary objectForKey:#"name"];
NSString *strBio=[DetailDictonary objectForKey:#"bio"];
// Or You can set it in Your ClassFile
MyClass *classObj=[[MyClass alloc] init];
classObj.strid=[DetailDictonary objectForKey:#"ID"];
classObj.strName=[DetailDictonary objectForKey:#"name"];
classObj.strBio=[DetailDictonary objectForKey:#"bio"];
[YourMainArray addObject:classObj]; //set YourClass to Array
[classObj release];
}

How to get foreign language text to display correctly in a UITextView

I have some text that isn't being displayed correctly in a UITextView. The "é", "ç" and "ñ" etc... are all being displayed like this:
The text is coming from a server and displays fine when viewed in a web browser.
Wondering how to go about fixing this. Thanks for any help.
here's the code:
// the deal id is passed from the offersvc table selected item and into the url
// dealID is 1020414
NSString *jsonDealString = [NSString stringWithFormat:#"http://api.**********.net/v1/public/details?id=%#&yd.key=******", dealId];
NSLog(#"jsondealstring is %#", jsonDealString);
// NSLog(#"deal id is %#",dealId);
// Download JSON
NSString *jsonString = [NSString
stringWithContentsOfURL:[NSURL URLWithString:jsonDealString]
encoding:NSStringEncodingConversionAllowLossy
error:nil];
// Create parser for the api
SBJSON *parser = [[SBJSON alloc] init];
NSDictionary *results = [parser objectWithString:jsonString error:nil];
[self setDisplayItems:[results objectForKey:#"results"]];
NSDictionary *item = (NSDictionary *)[displayItems objectAtIndex:0];
// NSLog(#"item from details is %#", item);
// set the labels and text views.
link = [item objectForKey:#"link"];
catStr = [item objectForKey:#"cat"];
vendorStr = [item objectForKey:#"vendor"];
titleStr = [item objectForKey:#"main_tag"];
You need to set the UTF-8 encoding.
EDIT in response of the modified question
Simply add NSUTF8StringEncoding like follows
NSString *jsonString = [NSString
stringWithContentsOfURL:[NSURL URLWithString:jsonDealString]
encoding:NSStringEncodingConversionAllowLossy|NSUTF8StringEncoding
error:nil];

How to output JSon data in Objective-C

I am currently working on an iPhone application that takes in data from the following source:
I am trying to figure out how to parse it into a human readable format in say a text field.
My code so far is:
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *urlString = [NSString stringWithFormat:#"http://dev.threesixtyapp.com/api/events.php?action=available&id=1"];
NSURL *url =[NSURL URLWithString:urlString];
NSData *data = [NSData dataWithContentsOfURL:url];
NSError *error;
NSMutableDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSLog(#"%#",json);
}
http://stig.github.com/json-framework/ - SBJson is a great framework for encoding/decoding JSON. I recommend you check it out...It will parse it for you into an NSDictionary, and you simply set the text of the textfield equal to the value in the NSDictionary that you want. It's pretty straightforward using this framework. Your Json should just be a string when you pass it to the SBJson functions btw
First of all you have to understand the data structure of your json.
You can use JSON Viewer to view the data structure of your json.
As I can see you are getting array of objects consisting of event_title, date_from and date_to.
NSError *error = nil;
NSArray *jsonArry = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSLog(#"%#",jsonArry);
for (NSDictionary *dict in jsonArry) {
NSString * title = [dict objectForKey:#"event_title"];
NSString * dateTo = [dict objectForKey:#"date_to"];
NSString * dateFrom = [dict objectForKey:#"date_from"];
NSLog(#"title=%#,dateTo=%#,dateFrom=%#",title,dateTo,dateFrom);
}

parsing JSON of webservice on objective c array

I'm developing an iphone app and I have a JSON from web service as below:
[
{
"0":"test_w",
"assignment_title":"test_w",
"1":"2011-11-02 04:02:00",
"assignment_publishing_datetime":"2011-11-02 04:02:00",
"2":"2011-11-02 01:53:00",
"assignment_due_datetime":"2011-11-02 01:53:00",
"3":"course_math.png",
"course_icon":"course_math.png",
"4":null,
"submission_id":null
},
{
"0":"\u062a\u0637\u0628\u064a\u0642 \u0631\u0642\u0645 3",
"assignment_title":"\u062a\u0637\u0628\u064a\u0642 \u0631\u0642\u0645 3",
"1":"2011-08-08 00:00:00",
"assignment_publishing_datetime":"2011-08-08 00:00:00",
"2":"2011-08-25 00:00:00",
"assignment_due_datetime":"2011-08-25 00:00:00",
"3":"course_math.png",
"course_icon":"course_math.png",
"4":null,
"submission_id":null
}
]
also I have a tableview and I need to parser assignment_title only on the tableview cells , also I'm using SBJSON library.
so what is the best way to extract assignment_title and put them on cells?
I find the solution from your answers as below:
I created a method with 2 parameters (json_path , field [that i need to show in tableview cell])
- (NSMutableArray*)JSONPath:(NSString *)path JSONField:(NSString *)field{
SBJSON *parser = [[SBJSON alloc] init];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:path]];
// Perform request and get JSON back as a NSData object
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
// Get JSON as a NSString from NSData response
NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
NSArray *statuses = [parser objectWithString:json_string error:nil];
NSMutableArray * tempMutArray = [[[NSMutableArray alloc] init] autorelease];
int i;
for (i=0; i<[statuses count]; i++) {
[tempMutArray addObject:[[statuses objectAtIndex:i] objectForKey:field]];
}
return [tempMutArray copy];
}
after that i call it in cell as following:
//in viewDidLoad
NSArray * homework = [self JSONPath:#"http://....." JSONField:#"assignment_title"];
//In cellForRowAtIndexPath
cell.textLabel.text = [homework objectAtIndex:indexPath.row];
Thanks to all
If you are doing it through NSJSONSerialization you can get array of assignment_title using this simple method ;)
NSError *error = nil;
NSData *jsonData = [NSData dataWithContentsOfURL:apiURL];
id jsonObjectFound = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
NSArray* assignmentTitles = [jsonObjectFound valueForKey:#"assignment_title"];
If performance matters, you might consider using an ASIHTTPRequest to fetch the json asynchronously, then inside the requestFinished: you might do something like:
- (void)requestFinished:(ASIHTTPRequest *)request
{
// Use when fetching text data
NSString *responseString = [request responseString];
//assuming you created a property instance variable NSArray *myArrAssignmentTitles
NSArray *tempArray = [responseString JSONValue];
//making an array of assignment_title
NSMutableArray *tempMutArray = [[NSMutableArray alloc] init];
int i;
for(i = 0;i < [tempArray count];i++){
[tempMutArray addObject:[[tempArray objectAtIndex:i] objectForKey:#"assignment_title"]];
}
//assign the data to the instance variable NSArray *myArrAssignmentTitles
self.myArrAssignmentTitles = tempMutArray;
//release tempMutArray since the instance variable has it
[tempMutArray release];
//call the reload table
[self.tableView reloadData];//i think this is how to reload the table
}
- (void)requestFailed:(ASIHTTPRequest *)request
{
NSError *error = [request error];
}
So, your myArrAssignmentTitles has all the values assignment_title from json
all you do is just apply the array data for the cell e.g.
cell.textLabel.text = [self.myArrAssignmentTitles objectAtIndex:indexPath.row];
its a long code sorry about that. But, thats works for me xD; it fetches the json asynchronously after that it creates an array of assignment_title hopes it helps.

Continuous conversion of text to speech from json data

I am designing an app that needs text to speech. I am using the library that is posted here to convert text to speech. I have to retrieve text from Json url, and pass the values to text to speech. I am able to retrieve Json data and convert it to text to speech for the ObjectatIndex 0 using the following code...
SBJSON *json = [[SBJSON alloc]init];
fliteEngine = [[FliteTTS alloc] init];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.sampleurl.txt"]];
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *jsonstring = [[NSString alloc]initWithData:response encoding:NSUTF8StringEncoding];
NSArray *asanasList = [json objectWithString:jsonstring error:nil];
NSArray *asanas =[asanasList objectForKey:#"yogagurubackpain"];
for(NSDictionary *test in asanas)
{
UrlValues *myasanas = [[UrlValues alloc]init];
myasanas.asanatitle = [test objectForKey:#"asanatitle"];
myasanas.asanatranscript = [test objectForKey:#"asanatranscript"];
myasanas.asanapicture = [test objectForKey:#"asanapicture"];
[data.yoga addObject:myasanas];
[myasanas release];
}
UrlValues *asana=[[data yoga]objectAtIndex:0];
self.AsanaName.text = [asana asanatitle];
self.AsanaTranscript.text = [asana asanatranscript];
NSString *imageUrl = [asana asanapicture];
NSString* mapUrl = [imageUrl stringByReplacingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSData* imageData = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:mapUrl]];
UIImage* image = [[UIImage alloc] initWithData:imageData];
self.AsanaImage.image = image;
NSString *speak = self.AsanaTranscript.text;
[fliteEngine setVoice:#"cmu_us_rms"];
[fliteEngine speakText:speak];
[fliteEngine setPitch:100.0 variance:11.0 speed:0.4];
[imageData release];
[image release];
[jsonstring release];
[json release];
Now my problem is how can i go to the next object automatically after completion of the playing of first one. The process must continue for all the objects. The corresponding images etc must load on the page after the text to speech is done for the first one... Plz help me...
Looking at the source of the linked library, it looks like you'll have to hack a method into FliteTTS.m. If you look at the source, it's using an AVAudioPlayer to play back a generated WAV file. It's also setting itself as the audio player's delegate. If you implement the audioPlayerDidFinishPlaying:successfully: delegate method and play the next chunk when it's called, you should be able to have a semi-continuous text-to-speech stream.