0
votes

I want to try to convert NSData to NSDictionary or NSArray but some how i can't do that but whenever try to convert into NSString it will converted and gating data so any one can know what is mistake ,help me below is code for convert

 NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data
                                                                   options:0
                                                                            error:nil];

Below code for convert into NSString

NSString *str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];

But Problem is that When get this type of data it will converted on array Response:

[["superwoman",0],["sugar",0],["sultan trailer",0],["summer",0],["superman",0],["sunday candy",0],["sublime",0],["summer sixteen",0],["superfruit",0],["sultan songs",0]]

but Whenever gat this type of response it will not converted on array formated because of ,[3 on response

Response:

[["ssundee",0],["selena gomez",0],["sia",0],["sorry beyonce",0],["smosh",0],["sweatshirt jacob sartorius",0],["skydoesminecraft",0],["secret life of pets trailer",0,[3]]

2
if your conversion returns nil the pass an error reference to NSJSONSerialization and debug.whats in the errorMuhammad Zohaib Ehsan
The last one is not a valid JSON, it's missing a closing "]" at the end.Larme
I suspect this is being converted from data to JSON in the wrong connection delegate method... else the server devs are idiots.Droppy
Getting this response from suggestqueries.google.com/complete/…Anil Makhija
As Droppy says: Either you are processing the JSON before it is complete, in which cause it is your fault, or the server devs produce something that is almost but not quite JSON.gnasher729

2 Answers

0
votes
try this one

    NSError* error;
                 NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseObject
                                                                      options:kNilOptions
                                                                        error:&error];
0
votes

1)You need to take the response in id type variable & check the class of response like below.

2)You also need to check if JSON convertion is successful

 NSError * error;

 id result = [NSJSONSerialization JSONObjectWithData:data 
      options:0 error:&error];

if (!error) {

 if([id isKindOfClass:[NSDictionary class]]){
   //Response is Dictonary
   } else if ([id isKindOfClass:[NSArray class]]){
   //Response is Array
   } else {
   //any other format
   }

 } else {

  //Handle error

 }