1

I am trying to get data from this link and store it in NSArray

Format of the response:

data(
[
"Mumbai, MH, India",
"Mumford, NY, United States",
"Mumford, TX, United States",
"Navi Mumbai, MH, India",
"New Mumbai, MH, India"
]
)

Tried as like regular JSON response, but not getting any data.

Please help me...

Thanks in advance.

Shardul
  • 4,266
  • 3
  • 32
  • 50
  • Post your relevant code so people can help you fix it. – rmaddy Feb 14 '14 at 06:35
  • There is a problem with the JSON in the link. Verify by posting the link inside sites like jsonlint.com first. – Anil Feb 14 '14 at 06:36
  • can u show the correct data, bz data( [ "Mumbai, MH, India", "Mumford, NY, United States", "Mumford, TX, United States", "Navi Mumbai, MH, India", "New Mumbai, MH, India" ] ) this is showing in wrong data – Anbu.Karthik Feb 14 '14 at 06:41
  • provide more information – codercat Feb 14 '14 at 06:43

7 Answers7

2

In your php may be return array value. so instead of that use json_encode and Returns the JSON representation of a value.

and use in Objective-C code like

 NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
codercat
  • 22,873
  • 9
  • 61
  • 85
2

I think this may be the problem ..

JSON that have array response will be [x, x, x, x, x].. But your json is data([x, x, x, x, x]); That's why you are not getting correct response in jsonserialization.

NSArray *response = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];

JSON that have dictionary response will be {key=data, key=data, key=data}

NSDictionary *response = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];

Your json gets the following error when you try to convert

(JSON text did not start with array or object and option to allow fragments not set.)

Your json not meets both the format. Can you please check it and try

RAJA
  • 1,214
  • 10
  • 13
2

http://gd.geobytes.com/AutoCompleteCity?callback=data&q=mum

This is a JSONP response, which wraps json response in callback function data.

Remove callback parameter and try to parse again

http://gd.geobytes.com/AutoCompleteCity?q=mum

See also: What is JSONP all about?

Community
  • 1
  • 1
vokilam
  • 10,153
  • 3
  • 45
  • 56
1

That doesn't look like JSON to my eyes, but you might be able to pass that input to NSJSONSerializer (I'm thinking the JSONObjectWithData:options:error: API) and get an Objective-C object out of it

Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
1

Try with following code,

NSString *jsonString = [[NSString alloc] initWithData:self.responseData encoding:NSUTF8StringEncoding];
NSMutableDictionary *jsonDictionary  = [jsonString JSONValue];
NSLog(@"%@", jsonDictionary);
iPatel
  • 46,010
  • 16
  • 115
  • 137
1

you can try this code.. this is not perfect.. but you can retrieve the data here.

NSError *err = Nil;
NSURL *url = [NSURL URLWithString:@"http://gd.geobytes.com/AutoCompleteCity?callback=data&q=mum"];
    NSData *data1 = [NSData dataWithContentsOfURL:url options:NSDataReadingMappedIfSafe error:&err];
    NSLog(@"%@",data1);
    NSString *json_string = [[NSString alloc] initWithData:data1 encoding:NSUTF8StringEncoding];
    NSLog(@"%@",json_string);
    NSString *json = [json_string substringFromIndex:4];
    NSLog(@"%@",json);
    NSCharacterSet *charsToTrim = [NSCharacterSet characterSetWithCharactersInString:@"()"];
    NSString *json1 = [json stringByTrimmingCharactersInSet:charsToTrim];
    NSLog(@"%@",json1);
    if ([json1 length]> 0)
        {
        json1 = [json1 substringToIndex:[json1 length]-2];
    }
    NSLog(@"returnResponse %@",json1);
    NSMutableArray *a = [[NSMutableArray alloc]init];
    [a addObject:[json1 componentsSeparatedByString:@","]];
    NSLog(@"%@",a);
    NSLog(@"%@",[a objectAtIndex:0]);
vokilam
  • 10,153
  • 3
  • 45
  • 56
gajchtr
  • 48
  • 5
1

I think your response string meeting JSON qualification JSON string will look like this

{
  "array": [
    1,
    2,
    3
  ],
  "boolean": true,
  "null": null,
  "number": 123,
  "object": {
    "a": "b",
    "c": "d",
    "e": "f"
  },
  "string": "Hello World"
}

make sure that response is getting from service is JSON string