I am having a problem storing JSON data into an Array of individual objects. It seems like the problem is in the execution of dispatch_asynch which is handling the JSON request. WHen I create a breakpoint before the method and than step through the application it seems to just fall through the block sent into dispatch_async.
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
NSError *error = nil;
NSURL *url = [NSURL URLWithString:@"http://sleepy-dusk-3603.herokuapp.com/companies.json"];
NSString *json = [NSString stringWithContentsOfURL:url
encoding:NSASCIIStringEncoding
error:&error];
NSLog(@"\nJSON: %@ \n Error: %@", json, error);
if(!error) {
NSData *jsonData = [json dataUsingEncoding:NSASCIIStringEncoding];
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:jsonData
options:kNilOptions
error:&error];
NSArray *tempArray = [NSArray arrayWithObjects:@"Call Support Desk", @"Call Genius Bar", nil];
for (NSString *name in [jsonDict valueForKeyPath:@"name"]) {
NSString *tempString = [[NSString alloc] initWithString:name];
Company *company = [[Company alloc] initWithName:tempString available_actions:tempArray];
[self addCompany:company];
I truly appreciate everyone's help and support with this issue.
if(!error) {
...
}
else
{
NSLog(@"%@",[error localizedDescription]);
}
Log the error and find what is happening
this code i tried and is working very well
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
NSError *error = nil;
NSURL *url = [NSURL URLWithString:@"http://sleepy-dusk-3603.herokuapp.com/companies.json"];
NSString *json = [NSString stringWithContentsOfURL:url
encoding:NSASCIIStringEncoding
error:&error];
NSLog(@"\nJSON: %@ \n Error: %@", json, error);
if(!error) {
NSData *jsonData = [json dataUsingEncoding:NSASCIIStringEncoding];
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:jsonData
options:kNilOptions
error:&error];
NSArray *tempArray = [NSArray arrayWithObjects:@"Call Support Desk", @"Call Genius Bar", nil];
for (NSString *name in [jsonDict valueForKeyPath:@"name"]) {
NSString *tempString = [[NSString alloc] initWithString:name];
NSLog(@"%@",tempString);
}
}
});
Response
2013-03-05 22:02:44.312 newTrial[4711:12303]
JSON: [{"created_at":"2013-03-04T00:09:06Z","id":1,"name":"Apple","updated_at":"2013-03-04T00:09:06Z","actions":[{"created_at":"2013-03-04T00:09:07Z","id":1,"name":"Call Support Desk","updated_at":"2013-03-04T00:09:07Z"},{"created_at":"2013-03-04T00:09:07Z","id":2,"name":"Call Genius Bar","updated_at":"2013-03-04T00:09:07Z"}]},{"created_at":"2013-03-04T02:01:49Z","id":2,"name":"Comcast","updated_at":"2013-03-04T02:01:49Z","actions":[{"created_at":"2013-03-04T02:01:49Z","id":3,"name":"Account Services","updated_at":"2013-03-04T02:01:49Z"}]}]
Error: (null)
2013-03-05 22:02:51.766 newTrial[4711:12303] Apple
So my assumption of the problem is at where you store the value to the array .Check it is properly initialised problem may be here
Company *company = [[Company alloc] initWithName:tempString available_actions:tempArray];
[self addCompany:company];
it works for me like a charm:
dispatch_queue_t _queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(_queue, ^{
NSError *_error = nil;
NSURL *_url = [NSURL URLWithString:@"http://sleepy-dusk-3603.herokuapp.com/companies.json"];
NSData *_json = [NSData dataWithContentsOfURL:_url];
if (_json) {
id _response = [NSJSONSerialization JSONObjectWithData:_json options:kNilOptions error:&_error];
if (!_error) {
// do whatever you want to do here...
} else {
NSLog(@"%@", _response);
}
}
});
the response is:
(
{
actions = (
{
"created_at" = "2013-03-04T00:09:07Z";
id = 1;
name = "Call Support Desk";
"updated_at" = "2013-03-04T00:09:07Z";
},
{
"created_at" = "2013-03-04T00:09:07Z";
id = 2;
name = "Call Genius Bar";
"updated_at" = "2013-03-04T00:09:07Z";
}
);
"created_at" = "2013-03-04T00:09:06Z";
id = 1;
name = Apple;
"updated_at" = "2013-03-04T00:09:06Z";
},
{
actions = (
{
"created_at" = "2013-03-04T02:01:49Z";
id = 3;
name = "Account Services";
"updated_at" = "2013-03-04T02:01:49Z";
}
);
"created_at" = "2013-03-04T02:01:49Z";
id = 2;
name = Comcast;
"updated_at" = "2013-03-04T02:01:49Z";
}
)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.