Notice
Recent Posts
Recent Comments
Link
투케이2K
191. (Objective-C/objc) [간단 소스] string to dictionary ( json ) 변환 방법 - 문자열 , 딕셔너리 본문
Objective-C
191. (Objective-C/objc) [간단 소스] string to dictionary ( json ) 변환 방법 - 문자열 , 딕셔너리
투케이2K 2025. 3. 3. 16:40[개발 환경 설정]
개발 툴 : XCODE
개발 언어 : OBJECTIVE-C

[소스 코드]
// --------------------------------------------------------------------------------------
[개발 및 테스트 환경]
// --------------------------------------------------------------------------------------
- 언어 : Objective-c
- 개발 툴 : Xcode
- 기술 구분 : objc / string / dictionary / json
// --------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------
[사전) 설명]
// --------------------------------------------------------------------------------------
1. 문자열이 JSON 형식일 경우 NSJSONSerialization 클래스를 사용해 딕셔너리로 변환할 수 있습니다.
2. NSJSONSerialization의 JSONObjectWithData:options:error: 메서드를 사용해 NSData를 NSDictionary로 변환합니다
// --------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------
[소스 코드]
// --------------------------------------------------------------------------------------
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
// [JSON 형식의 문자열]
NSString *jsonString = @"{\"key1\":\"value1\", \"key2\":\"value2\"}";
// [NSString을 NSData로 변환] : [UTF8 인코딩]
NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
// [NSData를 NSDictionary로 변환]
NSError *error;
NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
if (error) {
NSLog(@">>>>>>>>>>>>>>>>>> JSON parsing error :: %@ >>>>>>>>>>>>>>>>>>", error.localizedDescription);
} else {
NSLog(@">>>>>>>>>>>>>>>>>> Dictionary :: %@ >>>>>>>>>>>>>>>>>>", dictionary);
}
}
return 0;
}
// --------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------
[참고 사이트]
// --------------------------------------------------------------------------------------
[딕셔너리 사용해 json 생성 및 파싱 수행 실시 - NSMutableDictionary , NSJSONSerialization]
https://blog.naver.com/kkh0977/222654575075?trackingCode=blog_bloghome_searchlist
// --------------------------------------------------------------------------------------
반응형
'Objective-C' 카테고리의 다른 글
Comments