티스토리 뷰

반응형

javascript로 웹뷰와 통신중 json변환 및 확인 코드.


1
2
3
4
5
6
7
    NSDictionary *test = [NSDictionary dictionaryWithObjectsAndKeys:@"userid",@"id",@"123456",@"pw"nil];
    NSDictionary *test2 = [NSDictionary dictionaryWithObjectsAndKeys:test,@"login"nil];
    
    NSError *error;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:test2
                                                       options:NSJSONWritingPrettyPrinted error:&error];
    NSString *myString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
cs


// myString 에 대한 결과값

dic : {

  "login" : {

    "id" : "userid",

    "pw" : "123456"

  }

}


// Dictionary Json으로 변환.

1
2
3
4
5
6
7
8
9
10
-(NSString *)getJsonConverting:(id)obj {
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:obj
                                                       options:NSJSONWritingPrettyPrinted error:nil];
    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    jsonString = [jsonString stringByReplacingOccurrencesOfString:@" " withString:@""];
    jsonString = [jsonString stringByReplacingOccurrencesOfString:@"\n" withString:@""];
    jsonString = [jsonString stringByReplacingOccurrencesOfString:@"[]" withString:@""];
    
    return [jsonString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]];
}
cs

// getJsonConverting 에 대한 결과값

%7B%22login%22%3A%7B%22id%22%3A%22userid%22,%22pw%22%3A%22123456%22%7D%7D




반응형
댓글
댓글쓰기 폼
공지사항