Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 02-13-2011, 10:20 AM   #1 (permalink)
Registered Member
 
Join Date: Feb 2011
Posts: 14
Scrooby is on a distinguished road
Default Reading Json

Hi All,

I'm new to obj-c and trying to build my first app. I'm trying to parse some Json from a web service like the following format:

HTML Code:
{
    "data": {
        "2010-12": {
            "category1": {
                "rate": "xx", 
                "level": "xx", 
                "total": "xx"
            }, 
            "category2": {
                "rate": "xx", 
                "level": "xx", 
                "total": "xx"
            }
        },
        "2011-01": {
            "category1": {
                "rate": "xx", 
                "level": "xx", 
                "total": "xx"
            }, 
            "category2": {
                "rate": "xx", 
                "level": "xx", 
                "total": "xx"
            }
        }
    }
}
I've started writing the following in the connectionDidFinishLoading but from here I'm getting stuck:

HTML Code:
NSString *content = [[NSString alloc] initWithBytes:[responseData bytes] length:[responseData length] encoding:NSUTF8StringEncoding];
	
SBJSON *parser = [[SBJSON alloc] init];
NSDictionary *json = [parser objectWithString:content error:nil];
NSArray *months = [json objectForKey:@"data"];
	
for(NSDictionary *month in months){

}
	
[parser release];
Ideally my end result would be an NSArray of objects with each "category", "level", "rate" and "total" being properties of the object.

Any help with this would be much appreciated.

Thanks in advanced.

Scrooby
Scrooby is offline   Reply With Quote
Old 02-13-2011, 02:12 PM   #2 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

Judging from your JSON you don't have any arrays - everything appears to be a dictionary - so this line is wrong:

Code:
NSArray *months = [json objectForKey:@"data"];

The "months" variable is a dictionary. This would work:
Code:
NSDictionary *monthDict = [json objectForKey:@"data"];
Now you need to loop each key for that dictionary and create the objects you need.

Code:
for(NSDictionary *dateKey in monthDict){
     //dateKey contains "2010-12"
     NSDitionary *monthValues = [monthDict objectForKey:dateKey]; 
     Month *month = [[Month alloc] initWithdate:dateKey dictionary:monthValues];
     [objectArray addObject:month];
     [month release];
}
You'll need to create the Month class yourself, and decide if it keeps an array of categories or if it always has category1 and category2 properties or what.
__________________

Free Games!
smasher is offline   Reply With Quote
Old 02-13-2011, 03:11 PM   #3 (permalink)
Registered Member
 
Join Date: Feb 2011
Posts: 14
Scrooby is on a distinguished road
Default

That's a great help, much appreciated! I could do with a push in the right direction for the initWithDate method.

My end results needs to be an array of categories as the names of the could change.

Code:
for(NSDictionary *dateKey in monthDict){
     //dateKey contains "2010-12"
     NSDitionary *monthValues = [monthDict objectForKey:dateKey]; 
     Month *month = [[Month alloc] initWithdate:dateKey dictionary:monthValues];
     [objectArray addObject:month];
     [month release];
}
So based on the code you wrote and the original Json, my objectArray would have two Month objects ("2010-12" and "2011-01") then the Month object would contain an array which would have two items, one for each category, which would need to contain an object which contains the "category name", "rate", "level" and "total" values.

Any help you could offer would be great.

Thanks,

Scrooby
Scrooby is offline   Reply With Quote
Old 02-13-2011, 08:06 PM   #4 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

The initWithDate:dictionary: needs to do the same thing - loop through the keys, get the object, and pass it to the category init method.

Code:
//in your Month.h
NSMutableArray *categoryArray

//in your Month.m
- (id) initWithDate:(NSString*)date dictionary:(NSDictionary*)categoryDict
{
	self = [super init];
	if (self != nil) {

		categoryArray = [[NSMutableArray alloc] init];		 
	
		for (NSDictionary *catKey in categoryDict){
			//catKey is "category1"
			NSDictionary *attributes = [categoryDict objectForKey:catKey];
			Category *cat = [[Category alloc] initWithKey:catKey dictionary:attributes];
			[categoryArray addObject:cat];
			[cat release];
		}

	}
	return self;
}
EXTRA CREDIT: If you had a choice then this would be easier with arrays in the JSON. The structure would be easier to parse if all data is in values, not the keys, and your object structure could exatly mirror the JSON structure. There would be less mind-bending data juggling that way.
Code:
{
    "data": [
        {
            "month": "2010-12",
            "categories": [
                {
                    "category": "category1",
                    "rate": "xx",
                    "level": "xx",
                    "total": "xx" 
                },
                {
                    "category": "category2",
                    "rate": "xx",
                    "level": "xx",
                    "total": "xx" 
                } 
            ] 
        },
        {
            "month": "2011-01",
            "categories": [
                {
                    "category": "category1",
                    "rate": "xx",
                    "level": "xx",
                    "total": "xx" 
                },
                {
                    "category": "category2",
                    "rate": "xx",
                    "level": "xx",
                    "total": "xx" 
                } 
            ] 
        } 
    ]
}
__________________

Free Games!
smasher is offline   Reply With Quote
Old 02-14-2011, 03:18 PM   #5 (permalink)
Registered Member
 
Join Date: Feb 2011
Posts: 14
Scrooby is on a distinguished road
Default

That's fantastic, it works great!

Thanks for all your help!
Scrooby is offline   Reply With Quote
Reply

Bookmarks

Tags
json, nsarray, nsdictionary

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 394
14 members and 380 guests
blasterbr, buggen, Clouds, EvilElf, jeroenkeij, jimmyon122, LEARN2MAKE, Mah6447, n00b, nyoe, pungs, Sami Gh, stanny, toon4413
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,667
Threads: 94,121
Posts: 402,900
Top Poster: BrianSlick (7,990)
Welcome to our newest member, host number one
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 03:52 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0