2012-04-20 14 views
3

文字列をjsonオブジェクトとして配置する必要があり、オブジェクトを送信する前にこのjsonオブジェクトをhttpヘッダーに配置する必要があります。JSONオブジェクトとしての文字列の送信

「{」NID:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    NSString *nid = @""; 
    NSString *vocab = @""; 
    NSString *inturl = @"testoverview"; 
    NSString *mail = @"[email protected]"; 
    NSString *md5pw = @"4d57e7ef1b7c3f431aca424764e9d786"; 

    NSDictionary *jsonDictionary = [NSDictionary dictionaryWithObjectsAndKeys: 
            nid, @"nid", 
            vocab, @"vocab", 
            inturl, @"inturl", 
            mail, @"mail", 
            md5pw, @"md5pw",nil]; 


    NSString *json = @"{nid:"",vocab:"",inturl:testoverview, mail:[email protected], md5pw:4d57e7ef1b7c3f431aca424764e9d786}"; 


    NSError *error; 



    NSString *url = @"http://udv.taenk.dk/chh/drupal-taenk/services/mobile"; 


    NSData *data = [json dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]; 

    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonDictionary options:NSJSONWritingPrettyPrinted error:&error]; 


    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; 
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLCacheStorageAllowed timeoutInterval:30.0]; 

    NSURLConnection *connction = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 
    FSRemoteExecutor *remote = [[FSRemoteExecutor alloc] init]; 
    [remote execute:url andHandler:[FSProductTestHandler alloc] JSONString:jsonString JSONData:jsonData Connection:connction]; 
    [remote connection:connction didReceiveData:jsonData]; 
    [remote connectionFinishedLoading:connction]; 

私の問題は、私はサービスを受けるために持っている文字列フォーマットcuzの本で、オブジェクトとjsonDictionaryを使用して、それを送ることができないということです。これは私のコードです":"、 "vocab": ""、 "inturl": "testoverview"、 "mail": ""、 "md5pw": "}}"

辞書は文字列に=を挿入しますサービスからの応答を私に与えないでください。

私はdataWithJSONObjectなどの文字列(コードでJSONを)送りたい:JSOは、これを好き:

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:json options:NSJSONWritingPrettyPrinted error:&error]; 

が、私はこれをsatatingエラーが出る:

がキャッチされない例外のためにアプリを終了'NSInvalidArgumentException'、理由: '* + [NSJSONSerialization dataWithJSONObject:オプション:エラー:]:JSON書き込みに無効なトップレベルタイプ'

缶誰も私にこれを手伝ってくれますか?

答えて

12

しかし、なぜ使用するNSJSONSerialization? 独自のJSON文字列(NSString *json = @"{nid:"",vocab:"",inturl:testoverview, mail:[email protected], md5pw:4d57e7ef1b7c3f431aca424764e9d786}";)を作成しました。その後、再度実行する必要はありませんNSJSONSerialization

*json (NSString)からNSDataを作成してサーバーに送信してください。

は、このようなあなたのNSDataを行います

NSData *jsonPayload = [json dataUsingEncoding:NSUTF8StringEncoding]; 
+0

ありがとう、サービス応答データを取得して印刷する方法を教えていただけますか? – Lahib

4

まず、あなたの文字列jsonは、あなたはそれがないと思う引用文字が含まれていません。あなたは文字列を引用符で終わらせないために、文字列の引用符をバックスラッシュで "エスケープ"する必要があります。あなたのライン:

NSString *json = @"{nid:"",vocab:"",inturl:testoverview, mail:[email protected], md5pw:4d57e7ef1b7c3f431aca424764e9d786}"; 

は次のように解析されている:それは順序で3つの引用符で囲まれた文字列ですが、ある

NSString *json = @"{nid:" 
       ",vocab:" 
       ",inturl:testoverview, mail:[email protected], md5pw:4d57e7ef1b7c3f431aca424764e9d786}"; 

こと。コンパイラは、隣接する文字列リテラルを連結します。したがって、効果は次のようになります。

NSString *json = @"{nid:,vocab:,inturl:testoverview, mail:[email protected], md5pw:4d57e7ef1b7c3f431aca424764e9d786}"; 

埋め込み引用符なし。あなたが書きたかった何

た:あなたは「=」の文字を取得する場所、私は表示されません

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonDictionary options:NSJSONWritingPrettyPrinted error:&error]; 

NSString *json = @"{nid:\"\",vocab:\"\",inturl:testoverview, mail:[email protected], md5pw:4d57e7ef1b7c3f431aca424764e9d786}"; 

あなたはの出力を表示することができます。


更新:(あなたの質問から変更)

このコード:

NSString *nid = @""; 
NSString *vocab = @""; 
NSString *inturl = @"testoverview"; 
NSString *mail = @"[email protected]"; 
NSString *md5pw = @"4d57e7ef1b7c3f431aca424764e9d786"; 

NSDictionary *jsonDictionary = [NSDictionary dictionaryWithObjectsAndKeys: 
           nid, @"nid", 
           vocab, @"vocab", 
           inturl, @"inturl", 
           mail, @"mail", 
           md5pw, @"md5pw",nil]; 

NSError *error; 
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonDictionary options:NSJSONWritingPrettyPrinted error:&error]; 
NSString *resultAsString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; 
NSLog(@"jsonData as string:\n%@", resultAsString); 

は、この出力を与える:

jsonData as string: 
{ 
    "md5pw" : "4d57e7ef1b7c3f431aca424764e9d786", 
    "nid" : "", 
    "inturl" : "testoverview", 
    "mail" : "[email protected]", 
    "vocab" : "" 
} 

あなたが0代わりのNSJSONWritingPrettyPrintedを使用している場合、それが与えます:

jsonData as string: 
{"md5pw":"4d57e7ef1b7c3f431aca424764e9d786","nid":"","inturl":"testoverview","mail":"[email protected]","vocab":""} 
+0

jsonDictionaryの内容を印刷すると、オブジェクトとキーの間に「=」を付けて印刷します。 "="が入っているのはjsonDataの内容ではありません。 – Lahib

+0

だから?あなたは 'NSDictionary'がどのように自分自身を印刷するか気にしますか?それはあなたが気にするべきものに何の影響も与えません。 –

+0

このように文字列をフォーマットする必要があります。書いたように書式化されていない限り、サービスはそれを受け入れません。 – Lahib

関連する問題