-1

In my app, I am parsing the data using JSON

NSString * urlString=[NSString stringWithFormat:@"http://userRequest?userid=bala@gmail.com&latitude=59.34324&longitude=23.359257"];
NSURL * url=[NSURL URLWithString:urlString];
NSMutableURLRequest * request=[NSMutableURLRequest requestWithURL:url];
NSError * error;
NSURLResponse * response;
NSData *data=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString * outputData=[[NSString alloc]initWithData:data encoding:NSASCIIStringEncoding];
NSLog(@"%@",outputData);

SBJsonParser *jsonParser = [SBJsonParser new];
NSDictionary *jsonData = (NSDictionary *) [jsonParser objectWithString:outputData error:nil];
NSLog(@"%@",jsonData);
NSInteger success = [(NSNumber *) [jsonData objectForKey:@"success"] integerValue];  

After this code executes, In my log it is printed as

({  
latitude = "0.000000000000000";  
longitude = "0.000000000000000";  
username = sunil;  
},  
{  
latitude = "80.000000000000000";  
longitude = "30.000000000000000";  
username = arun;  
})  

But while running, the app crashes, as

'NSInvalidArgumentException', reason: '-[__NSArrayM objectForKey:]: unrecognized selector sent to instance 0x910d8d0'
chandru
  • 407
  • 1
  • 5
  • 26
  • the above code is just fine, the error is somewhere else in your code. please show us the instantiation of your array and the code where you try to access the data in your array since you are trying to call a method on your `NSMutableArray` which only exists for `NSMutableDictionary` – dehlen Jan 02 '14 at 09:49
  • oh, sorry, I will update it – chandru Jan 02 '14 at 09:50
  • According to my knowledge, in json ':' is used instead of '='.. Let me know if i am wrong – Prince Agrawal Jan 02 '14 at 09:55

4 Answers4

1

I think your problem is, that jsonParser objectWithString returns an array with dictionaries in it not dictionaries itself.

Try the following:

NSArray *jsonData = (NSArray *) [jsonParser objectWithString:outputData error:nil];

for(NSDictionary *dict in jsonData) {
    NSLog(@"%@",dict);
}

Does that work for you ?

Bhumeshwer katre
  • 4,671
  • 2
  • 19
  • 29
dehlen
  • 7,325
  • 4
  • 43
  • 71
  • now my dictionary contains many coordinates, I want to plot them, to google maps, any help – chandru Jan 02 '14 at 10:25
  • udpate your question or make a new question with detailed description of your problem and what you have tried until now. – dehlen Jan 03 '14 at 09:13
  • can u help me in this http://stackoverflow.com/questions/20902732/how-to-plot-the-markers-in-google-maps-from-a-dictionay-in-ios – chandru Jan 04 '14 at 06:07
1

Your reponse is NSArray which contains NSDictionary. So frst get dictionary from array then access value. Also Your json not look like correct.

for (NSDictionary *dict in responseArray) {
    double latitude = [dict[@"latitude"]doubleValue];
    double longitude = [dict[@"latitude"] longitude];
    NSString* name = dict[@"username"];
}
Bhumeshwer katre
  • 4,671
  • 2
  • 19
  • 29
1

1. First of all you are getting NSArray in JSON

JSON Starts with "(" means NSArray

JSON Starts with "{" means NSDictionary

Here you are getting NSArray which has collection of NSDictionary,

    {
        latitude = "0.000000000000000";
        longitude = "0.000000000000000";
        username = sunil;  
    },...

2."success" key is not present in the JSON..

  • Fix

    NSArray *jsonData = (NSArray *) [jsonParser objectWithString:outputData error:nil];
    
    If([jsonData count]>0){
    
        // Has some data
        // Iterate NSDictionary and get data here
    }
    else{
    
        // No Data
    }
    
  • now my dictionary contains many coordinates, I want to plot them, to google maps, any help – chandru Jan 02 '14 at 10:25
  • can U please help me with this http://stackoverflow.com/questions/20902732/how-to-plot-the-markers-in-google-maps-from-a-dictionay-in-ios – chandru Jan 04 '14 at 06:08
0

some where you are getting data from nsarray with using some object key. that key is invalid to fetching data from array

codercat
  • 22,873
  • 9
  • 61
  • 85