-1

Possible Duplicate:
How to use NSJSONSerialization

I am testing to use the web service of my website on iphone Application. The JSON with problem is that:

[
    {
        "name": "Jason1",
        "age": 20
    },
    {
        "name": "Jason2",
        "age": 40
    },
    {
        "name": "Jason3",
        "age": 60
    }
]

And my codes:

   NSData *jasonData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://localhost:3000/all_personal_information.json"]];
    NSDictionary *json = nil;
    if (jasonData) {
        json = [NSJSONSerialization JSONObjectWithData:jasonData options:kNilOptions error:nil];
    }

The code work fine with {"name":"jason","age":20} and I can get the values by using json[@"name"] and json[@"age"]

But i don't know how to get the value from the JSON with problem. I tried to use [json enumerateKeysAndObjectsWithOptions] to transverse the dictionary. But I will get an error:

enumerateKeysAndObjectsWithOptions:usingBlock:]: unrecognized selector sent to instance 0x89b2490

But I can get the full JSON when I Log the [json description] into console.

Community
  • 1
  • 1
code4j
  • 4,208
  • 5
  • 34
  • 51

3 Answers3

2

You're creating a dictionary while you get an array. If you do the following it should work:

id json = nil;
if (jasonData)
{
    json = [NSJSONSerialization JSONObjectWithData:jasonData options:kNilOptions error:nil];
}

if ([json isKindOfClass:NSArray.class])
{
    for (id personDef in json)
    {
        if ([personDef isKindOfClass:NSDictionary.class])
        {
            NSDictionary * dict = (NSDictionary *) moduleDef;

            NSString * name  = [dict objectForKey:@"name" withClass:NSString.class];

            NSLog(@"Person: @%", name);
        }
    }
}

In here I do some additional checking if the objects are the ones we expect. If this isn't the case you should add (proper) error handling.

Piro
  • 2,476
  • 3
  • 17
  • 19
  • Good try :) I hope I understand your code and fixed some syntax mistake. But I still cannot finish the task.[link]http://chopapp.com/#n5y1m9tk[link]. the label shows that this is not NSArray. – code4j Oct 06 '12 at 07:50
  • OOO I made a mistake, I forgot to turn on my server – code4j Oct 06 '12 at 08:00
2

Take it in an array.. for example

NSData *jasonData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://localhost:3000/all_personal_information.json"]];
NSDictionary *json = nil;
if (jasonData) {
    NSError *e = nil;
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:jasonData options:NSJSONReadingMutableContainers error: &e];
}

the array will contain your

{
 "name": "Jason1",
 "age": 20
}

etc in its individual indexes. when u want to get the values in it, you can use this below method to get the values

NSDictionary *userName = [jsonArray objectAtIndex:1];
NSString *stringName = [userName valueForKey:@"name"];
Sharanya K M
  • 1,805
  • 4
  • 23
  • 44
1

it will help you.

     NSMutableDictionary *CompaintsAry =[NSJSONSerialization JSONObjectWithData:respo options:kNilOptions error:&error];
     NSMutableArray *tempary =[[NSMutableArray alloc]init];
    for (int i=0;i < [CompaintsAry count];i++) {
    CfResultFatch *rs = [[CfResultFatch alloc] initWithName:[[CompaintsAry obj ectAtIndex:i]objectForKey:@"Name"]
                                                     cipd :[[CompaintsAry objectAtIndex:i] objectForKey:@"Age"]];

    [tempary addObject:rs];
}
cfComlaintsLists = [[NSMutableArray alloc] initWithArray:tempary];
SelectComplain = [[NSMutableArray alloc] initWithCapacity:[cfComlaintsLists count]]; 
[chiftab reloadData];
Bittu
  • 371
  • 4
  • 17
  • 1
    Beware, there are 4 unreleased objects here. – Cyrille Oct 06 '12 at 07:43
  • 1
    I did not test it because I don't know what is CfResultFatch. But thanks for you answer :) – code4j Oct 06 '12 at 08:15
  • What is `CfResultFatch`? Using Google, I haven't been able to find any useful reference to this class or even the selector `initWithName:cipd:`, though I have found similar sample code on other sites. – Nate Chandler Dec 26 '12 at 16:47