简体   繁体   中英

Parse coordinates from JSON response

I have JSON with data

> {
>     "a": {
>         [{"X":12,"Y":6},{"X":24,"Y":9},{"X":91,"Y":23},{"X":36,"Y":79},{"X":69,"Y":71},{"X":55,"Y":19}],
>         "roam": true,
>         "device": true
>     } }

I used

- (void)viewDidLoad {   
    [super viewDidLoad];

    responseData = [[NSMutableData data] retain];       
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://josn-example/json.json"]];
    [[NSURLConnection alloc] initWithRequest:request delegate:self];            
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    [responseData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [responseData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    label.text = [NSString stringWithFormat:@"Connection failed: %@", [error description]];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {      
    [connection release];

    NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
    NSLog(@"Response String : %@", responseString);

    [responseData release];

    NSError *error;
    SBJSON *json = [[SBJSON new] autorelease];
    NSArray *luckyNumbers = [json objectWithString:responseString error:&error];
    NSLog(@"luckNumber : %@",luckyNumbers);

    [responseString release];   

    if (luckyNumbers == nil)
        label.text = [NSString stringWithFormat:@"JSON parsing failed: %@", [error localizedDescription]];
    else {      
        NSMutableString *text = [NSMutableString stringWithString:@"Lucky numbers:\n"];

        for (int i = 0; i < [luckyNumbers count]; i++) 
            [text appendFormat:@"%@\n", [luckyNumbers objectAtIndex:i]];

        label.text =  text;
    }
}

I am trying to parse the X and Y coordinates and put a imageview on those coordinates. Please advise how I can achieve this.

Thanks

First off, The JSON snippet that is shown above is not a valid JSON. Here is a valid counterpart { "a": [ { "X": 12, "Y": 6 }, { "X": 24, "Y": 9 }, { "X": 91, "Y": 23 }, { "X": 36, "Y": 79 }, { "X": 69, "Y": 71 }, { "X": 55, "Y": 19 } ], "roam": true, "device": true }.

Assuming you have a valid JSON, you can then use NSJSONSerialization to convert the string to a FOundation object (NSDictionary) that you can then easily process. Sample snippet (assume "json" holds the json String:

    NSData* jsonData = [json dataUsingEncoding:NSUTF8StringEncoding];
    NSError* error ;
    id jsonObj = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:&error];
    NSLog (@"JSON Is %@ : Error is %@",jsonObj, error);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM