Reputation: 29064
I have seen that most data in iOS is in either JSON or XML. I was interested to know whether there is any third party library or wrapper class that tackles both types of data and responses accordingly. I know I could use for example JSONKit for JSON data and NSXMLParser for XML. But i looking for one that tackles both.
Is there any such wrapper?
Welcome to any suggestions and guidance. Thanks.
Upvotes: 0
Views: 390
Reputation: 652
I have created one wrapper class for json. In there, I have created one method given below, I think it will help you.
Parse Method:
-(void)jsonDeserialize:(NSString *)key fromDict:(id)content completionHandler:(void (^) (id parsedData, NSDictionary *fromDict))completionHandler{
if (key==nil && content ==nil) {
completionHandler(nil,nil);
}
if ([content isKindOfClass:[NSArray class]]) {
for (NSDictionary *obj in content) {
[self jsonDeserialize:key fromDict:obj completionHandler:completionHandler];
}
}
if ([content isKindOfClass:[NSDictionary class]]) {
id result = [content objectForKey:key];
if ([result isKindOfClass:[NSNull class]] || result == nil) {
NSDictionary *temp = (NSDictionary *)content;
NSArray *keys = [temp allKeys];
for (NSString *ikey in keys) {
[self jsonDeserialize:key fromDict:[content objectForKey:ikey] completionHandler:completionHandler];
}
}else{
completionHandler(result,content);
}
}
}
Method Call:
NSData *content = [NSData dataWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"Sample" ofType:@"json"]];
NSError *error;
//to get serialized json data...
id dictionary = [NSJSONSerialization JSONObjectWithData:content options:NSJSONReadingMutableContainers error:&error];
//get data for key called GetInfo
[self jsonDeserialize:@"GetInfo" fromDict:dictionary completionHandler:^(id parsedData, NSDictionary *fromDict) {
NSLog(@"%@ - %@",parsedData,fromDict);
}];
Upvotes: 0
Reputation: 1953
AFNetworking will do both, and has a goot reputation, for example see the Ray Wenderlich site which has lots of other useful information too.
Upvotes: 0
Reputation: 315
I think It's support to JSON /XML,just can you change some line.
NSString *str1=@"type url";
NSString *poststr1 = [NSString stringWithFormat:@"%@",str1];
NSString *posturl1=[NSString stringWithFormat:@" your url json/xml"];
// NSLog(@"city url name %@",posturl1);
NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"%@",posturl1]];
NSData *postData1 = [poststr1 dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData1 length]];
NSMutableURLRequest *request =[[[NSMutableURLRequest alloc] init] autorelease];
[request setHTTPBody:postData1];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
NSError *error;
NSURLResponse *response;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
// NSString *data=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];//this line supprted to json
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:urlData
options:kNilOptions
error:&error];//this line supported to xml
NSLog(@"json %@",json);
//NSDictionary*results = [data JSONValue];//this line supported to JSON
//NSLog(@"results json----->%@",results);
Upvotes: 1