2012-08-01 3 views
0

JSON 개체를 만들 수 있어야합니다 (iOS 5에 내장 된 클래스를 사용하고 있습니다).iOS의 JSON 요청에 JSON blob 추가

{"request_type":"<the request type (a string)>" "security_level":<the security level (an int)> "device_type":"<android or ios (a string)" "version":<the app version (android, ios on different version schemes) (a float)> "email":<the email address of the user sending the request (a string)> "blob":{<the contents of the actual request, encrypted/encoded according to the security level (a map)>} 

내 문제, 즉 마지막 부분에 "덩어리"기본적으로 또 다른 JSON 객체입니다 , 함께

{"display_name":"Jack Bower", "email":"[email protected]", "password":"roflcopter"} 

(의는 잊어 보자 전송해야 JSON이있다 암호는 일반 텍스트)

NSDictionary를 사용하여 모든 것을 만들 수 있습니다. 마지막 부분을 추가하는 방법을 모르겠습니다.

내 생각에 NSDictionary를 사용하여 첫 번째 요청을 만듭니다. 그런 다음 다른 NSDictionary를 사용하여 두 번째 BLOB 요청을 만듭니다. 그리고 두 번째 BLOB NSDictionary를 초기 NSDictionary에 다시 객체로 추가하십시오.

NSJSONSerialization은 내가하려는 것을 이해합니까?

답변

0

좋아 나는 오전 5시 생각 난 정말 바보 야 :

NSDictionary *blobData = [NSDictionary dictionaryWithObjectsAndKeys: 
          @"email",userEmail, 
          @"password",userPassword, 
         nil]; 
NSString *blobString = [[NSString alloc] 
         initWithData:[NSJSONSerialization dataWithJSONObject:blobData options:kNilOptions error:&error] 
         encoding:NSUTF8StringEncoding]; 
NSDictionary *requestData = [NSDictionary dictionaryWithObjectsAndKeys: 
          @"login",@"request_type", 
          0,@"security_level", 
          @"ios",@"device_type", 
          @"blob",blobString, 
          nil]; 
NSData *JSONRequestData = NULL; 
if ([NSJSONSerialization isValidJSONObject:requestData]) { 
     NSData *JSONRequestData = [NSJSONSerialization dataWithJSONObject:requestData options:kNilOptions error:&error]; 
} 
else NSLog(@"requestData was not a proper JSON object"); 
: 여기

가 대답