당신이 이런 식으로 데이터를 처리 할 수있는 웹에서 데이터 문자열 (아마 JSON) (에서 iOS 5) 왔는지 경우 대신
- (void)processData:(NSData *)responseData {
NSError* error;
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:responseData
options:kNilOptions
error:&error];
NSString* city = [[json objectForKey:@"Address"] objectForKey:@"City"];
NSString* country = [[json objectForKey:@"Address"] objectForKey:@"Country"];
NSString* result = [city stringByAppendingFormat:@", %@",country];
NSLog(@"%@", result); //New York, United States
}
을 이 캐릭터 라인이 사전 표현 인 경우, 올바른 형식은 다음과 같습니다
NSString *[email protected]"Address = {"
@"City = \"New York\";"
@"Country = \"United States\";"
@"CountryCode = us; };";
을 그래서 당신은 정말 당신이 이런 식으로 NSPropartyListSerialization를 사용할 수있는 NSDictionary에있는 NSString에서 전달하려면 :
(업데이트 된 질문에 따라)
NSError* error;
NSData *dat=[str dataUsingEncoding:NSUTF8StringEncoding];
NSPropertyListFormat plistFormat;
NSDictionary *temp = [NSPropertyListSerialization propertyListWithData:dat options:NSPropertyListImmutable format:&plistFormat error:&error];
NSString* city = [[temp objectForKey:@"Address"] objectForKey:@"City"];
NSString* country = [[temp objectForKey:@"Address"] objectForKey:@"Country"];
NSString* result = [city stringByAppendingFormat:@", %@",country];
NSLog(@"%@",result);
편집 : 당신이 무엇을 게시
는 사전되지 배열입니다. 사전은 키 - 값으로 식별되는 요소 집합으로 구성됩니다. 배열은 인덱스로 식별되는 요소 집합으로 구성됩니다. 따라서 배열 내에있는 요소가 문자열 인 경우 각 문자열의 구문 분석 작업을해야합니다. 이 @FelixKling 말했듯이, 당신은 또한 JSON과 같은 표준 형식에서 작동해야, 일반적으로 가장 좋은 방법이 아니다, XML 등 당신이에서 문자열을 가지고 않습니다
출처
2012-05-01 12:12:51
Mat
? JSON과 같은 표준 데이터 형식을 사용하지 않는 이유는 무엇입니까? 모든 것을보다 쉽게 해줍니다. –
코드에는이 문자열에 대한 사전 객체가 있어야합니다. 그래서 난 내 대답 (내 편집)를 참조하십시오 @JeanPaulScott 질문 – Nit
를 코드를 제시해주십시오. –