iOS 앱에서 JSON 문자열을 구문 분석하는 방법은 무엇입니까? SBJSON 사용. 아래 코드를 실행합니다. 데이터를 얻는 것은 성공적이지만 JSON 문자열에 항목이 들어 있어도 배열의 항목 수는 0입니다. 내 질문에 루프에서 JSON 문자열을 쿼리하는 방법은 무엇입니까?iOS : JSON String to NSArray가 예상대로 작동하지 않습니다.
감사합니다.
// Create new SBJSON parser object
SBJsonParser *parser = [[SBJsonParser alloc] init];
// Prepare URL request to download statuses from Twitter
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://safe2pee.org/api/proxlist.php?location=37.7626214,-122.4351661&distance=1"]];
// Perform request and get JSON back as a NSData object
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
// Get JSON as a NSString from NSData response
NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
NSLog(@"string equal = %@", json_string);
// parse the JSON response into an object
// Here we're using NSArray since we're parsing an array of JSON b objects
NSArray *bathrooms = [parser objectWithString:json_string error:nil];
NSLog(@"count = %d", [bathrooms count]);
// Each element in bathrooms is a single bathroom
// represented as a NSDictionary
for (NSDictionary *bathroom in bathrooms)
{
// You can retrieve individual values using objectForKey on the bathroom NSDictionary
// This will print the tweet and username to the console
NSLog(@"%@ - %@", [bathroom objectForKey:@"name"], [[bathroom objectForKey:@"lat"] objectForKey:@"long"]);
}
'[json_string JSONValue]'를 시도해보십시오. –