Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jan 2, 2013, 09:35 AM   #1
dankiiing
macrumors newbie
 
Join Date: Oct 2012
Parsing JSON value on UIPickerview xcode

I have a pickerview with which I would want to display values using JSON parsing. I am aware some questions on the site already tackle parsing and pickerview but mine is slightly different in regards to the structure of my json file. Here's a sample of the json file.
Code:
[
{
"model":"juice",
"id" :
  [
    {
        "version": "version01"
    },

    {
        "version": "version02"
    }

  ]
},
 {
"model":"cream",
"id" :
[
  {
      "version": "cream01"
  },

  {
      "version": "cream02"
  }
]
}
]
and my .m file

Code:
- (void)viewDidLoad
    {
    [super viewDidLoad];

    NSURL * serverhost = [NSURL URLWithString:@"http://my.json"];

    NSError *error;

    NSData  * Data  = [NSData  dataWithContentsOfURL: serverhost];

    self.modelsArray= [NSJSONSerialization JSONObjectWithData:Data   options:NSJSONReadingAllowFragments error:&error];

    NSLog(@"%@", modelsArray);
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 2;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component
{
    if (component == departments)
        return [self.modelsArray count];
    return [self.versionsArray count];
}


#pragma mark Picker Delegate Methods
- (NSString *)pickerView:(UIPickerView *)pickerView
         titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (component == departments)
          return [[self.modelsArray    objectAtIndex:row]objectForKey:@"model"];
    return [[[self.versionsArray objectAtIndex:row]objectForKey:@"model"]objectForKey:@"version"];
}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:    (NSInteger)component
{
    if (component == departments)
    {
        NSString * selectedRow = [[self.modelsArray objectAtIndex: row]    valueForKey:@"version"];
        NSArray * array = [sybreDepts objectForKey:selectedRow];
        self.
    }
}

@end
Basically when I select a model i.e juice or cream on the first column the second column should display its contents with key being 'versions' thus when juice is selected "version01" and "version02" should be displayed on the second column.

In regards to the line return
Code:
[[[self.versionsArray objectAtIndex:row]objectForKey:@"model"]objectForKey:@"version"];
I don't believe it was executed correctly as I get an error
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString objectForKey:]: unrecognized selector sent to instance 0xa18c450' .

Any suggestions ? thanks
dankiiing is offline   0 Reply With Quote
Old Jan 2, 2013, 09:41 AM   #2
dejo
Moderator
 
dejo's Avatar
 
Join Date: Sep 2004
Location: The Centennial State
What is the type of [self.versionsArray objectAtIndex:row]? I suspect it's an NSString, therefore you can't call objectForKey: on it.
__________________
dejo is offline   0 Reply With Quote
Old Jan 2, 2013, 09:55 AM   #3
dankiiing
Thread Starter
macrumors newbie
 
Join Date: Oct 2012
Thanks for you reply Dejo, im fairly new to json parsing , how do you suggest I fix this ? I had been on this for weeks
dankiiing is offline   0 Reply With Quote
Old Jan 2, 2013, 10:09 AM   #4
dejo
Moderator
 
dejo's Avatar
 
Join Date: Sep 2004
Location: The Centennial State
Quote:
Originally Posted by dankiiing View Post
...how do you suggest I fix this ?
Reevaluate what you are storing in self.versionsArray. Your code suggests you are expecting it to be NSDictionary while the error suggests it is NSString.

How much do you know about basic debugging (breakpoints, examining variables values, etc.)?
__________________
dejo is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
JSON Parsing and put in TableView+Hierarchy mehdies iPhone/iPad Programming 2 Nov 30, 2013 10:28 AM
Resolved: Need help: UIPIckerView and JSON lasash iPhone/iPad Programming 13 Sep 30, 2012 10:13 AM
Need help parsing simple JSON in Xcode Hustler1337 iPhone/iPad Programming 8 Aug 3, 2012 09:00 PM
JSON Parsing Issue quotes and parenthesis mngoose iPhone/iPad Programming 12 Jun 14, 2012 11:03 AM

Forum Jump

All times are GMT -5. The time now is 03:06 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC