본문 바로가기

개발도구/iOS - 아이폰 개발

[iphone] create Json like twiiter json

iPhone SDK: First Steps With JSON Data Using the Twitter API

http://mobile.tutsplus.com/tutorials/iphone/iphone-json-twitter-api/

을이용하여

NSDictionary *contactData = [NSDictionary dictionaryWithObjectsAndKeys: 
                                 textf1.text, @"value1", 
                                 textf2.text, @"value2", 
                                 textf3.text, @"value3",                                  
                                 nil]; 
    NSDictionary *finalData = [NSDictionary dictionaryWithObject:contactData forKey:@"contact"];
    NSString *newJSON = [contactData JSONRepresentation]; 

왜 이것을 찾게 되었냐면.. 자꾸 값이
(
{
value1 = 123;
value2 = 123;
value3 = 11123;
}
)

이렇게 만들어 지는게 아닌가.
그래서 JSONRepresentation 을 통해 { , , } json형태로 만들었다.

더불어 A을 B 로 만들기

[{
"memberid":"18",
"useridFK":"30",
"loginName":"Johnson",
"name":"Frank",
"age":"23",
"place":"School",
},

It needsthe following format:

[{"memberid":"18" {
 
"useridFK":"30",
   
"loginName":"Johnson",
   
"name":"Frank",
   
"age":"23",
   
"place":"School",}
 
},



을 이용하면 만들수 있다.

또한 위의JSON을 넣은후 파싱하는건 아래의 소스와 같다.

JSON의 예제는 이렇다. //[[[{"uid":"0","username":"admin","password":"REMOVED","email":"REMOVED","cash":"925071","exp":"117500","level":"1","clan":"YES","clanid":"1"}]]]
 
    //parse our json
    SBJSON *parser = [[SBJSON alloc] init];   
   
    //정보
   
    NSDictionary *description = (NSDictionary *)rss.description;   
    [parser release];
    NSLog(@"description=%@",description);
  
   /*
    NSData *jsonData = [description dataUsingEncoding:NSUTF8StringEncoding];
    NSError *error = nil;

     NSDictionary *dictionary = [[CJSONDeserializer deserializer] deserializeAsDictionary:jsonData error:&error];
    NSLog(@"dictionary=%@",dictionary);
     NSLog(@"Cash: %@", [dictionary objectForKey:@"value1"]);   
    */
   
    NSError *error = nil;
    NSData *jsonData = [rss.description dataUsingEncoding:NSUTF8StringEncoding];
    NSLog(@"jsonData%@",jsonData);
    NSArray *array = [[CJSONDeserializer deserializer] deserializeAsArray:jsonData error:&error];
    NSLog(@"array%@",array);
   

for 문은 [[[{ , ,  }]]] 대괄호을 끄집어 내기 위한 작업이다
    for (NSArray *innerArray2 in array) {
        for (NSArray *innerArray3 in innerArray2) {
            for (NSDictionary *dictionary in innerArray3) {
                NSString *cash = [dictionary objectForKey:@"cash"];
                NSLog(@"Cash = %@", cash);
            }
        }
    }