2009년 8월 13일 목요일

[iPhone] JSON 사용하기

- (NSString *)stringWithUrl:(NSURL *)url
{
 NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url
                                                    cachePolicy:NSURLRequestReturnCacheDataElseLoad
                                                    timeoutInterval:30];
     // Fetch the JSON response
 NSData *urlData;
 NSURLResponse *response;
 NSError *error;
 
 // Make synchronous request
 urlData = [NSURLConnection sendSynchronousRequest:urlRequest
                                        returningResponse:&response
                                                    error:&error];
 
  // Construct a String around the Data from the response
 return [[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding];
}

 

- (id) objectWithUrl:(NSURL *)url
{
 SBJSON *jsonParser = [SBJSON new];
 NSString *jsonString = [self stringWithUrl:url];
 
 // Parse the JSON into an Object
 return [jsonParser objectWithString:jsonString error:NULL];
}

 

- (NSDictionary *) downloadPublicJaikuFeed
{
 id response = [self objectWithUrl:[NSURL URLWithString:@"http://jaiku.com/feed/json"]];
 
 NSDictionary *feed = (NSDictionary *)response;
 return feed;
}

 

[Test Section]

NSDictionary *feed = [self downloadPublicJaikuFeed];
NSLog(@"Here is the title of the feed: %@", [feed valueForKey:@"title"]);

 

[환경 설정]

1. Download the DMG http://json-framework.googlecode.com/files/JSON_2.1.1.dmg
2. Mount the DMG and copy the directory /Volumes/JSON_2.1.1/SDKs/JSON to ~/Library/SDKs/JSON
3. Now, in the build properties (for both Debug and Release) there are two additional properties to configure (omit the double quotes bellow) …

  a “Additional SDKs” must be set to “$HOME/Library/SDKs/JSON/$(PLATFORM_NAME).sdk”
  b “Other Linker Flags” must be set to “-ObjC -ljson”
4. In the source file that uses the JSON library, add the following import “#import <JSON/JSON.h>”

 

[ 한글판 ]

1. SDKs 디렉토리를 ~/Library/SDKs로 복사합니다.
2. JSON.framework 디렉토리를 ~/Library/Frameworks로 복사합니다.
3. 프로젝트 설정창에서 Additional SDKs안에 $HOME/Library/SDKs/JSON/$(PLATFORM_NAME).sdk를 추가합니다.
4. Other Linker Flags-ObjC -ljson를 추가합니다.

 

여기서 중요한 것은 SDK 3.0에서는 에러가 발생한다는 것이다.

Error: Syntax error before 'AT_NAME' token 3.0

 

이러한 에러를 포함 2개가 발생한다.

따라서, Debug를 2.2.1로 변경을 해주어야 한다.

Groups & Files에서 프로젝트 제목이 붙은 부분을 더블 클릭 -> "Gerneral" Tab을 클릭

-> Base SDK for All Configurations: 부분을 클릭 하여 iPhone Simulator 2.2.1을 선택

-> 그 후 실행 해 본다.

 

SDK 3.0에서 돌아가게 하는 방법은

GCC를 변경 해주는 것이다. 환경 설정 부분에 보면 GCC가 4.2로 되어 있는데,

그 부분을 GCC 4.2(System Default) 부분으로 변경 후, 다시 GCC 4.2로 하면.. Error 해결!

^^; 원인은 모르겠지만..... GCC 문제임은 확실 하다! ^^

 

[참고 사이트]

http://iphonedevelopertips.com/cocoa/json-framework-for-iphone-part-2.html

http://theeye.pe.kr/entry/how-to-use-json-framework-on-iphone-sdk

 

댓글 2개:

  1. 참 좋은 내용이군요(이해는 못하지만)...그래도....안나와요...

    그래도...안나와요.....

    그래도...안나와요.....

    아놔...OTL....정말 언제쯤 볼수 있으려나요...아이폰...

    답글삭제
  2. @엘프화가 - 2009/08/28 12:59
    ㅠㅠ 아.. 아이폰.. 빨리 보고 싶은뎁;;

    저도 기다리고 있는 사람 중 하나입니다..

    아.. 제발 제발 제발..

    물떠놓고 밤에 자기전마다 빌어야 할듯해용!! ^^



    상단은.. 아이폰 어플 개발 관련..;; 내용이예요~

    답글삭제