2012-03-05 44 views
0

我正在使用AFNetworking,我試圖發佈一個JSON結構。問題是,而不是像{"my_property":"my value"},它的格式爲{my_property:'my_value'}。我猜在大多數情況下,第一組引用的損失是可以的,但我不確定如何處理非JSON單引號,並且對於它爲什麼會生成單引號的問題感到非常困惑它從NSDictionary創建JSON。此外,它包括[對象]參考,我只是期望一個「{」。這是服務器越來越:AFNetworking發佈格式不正確的JSON - 單引號和[對象]參考

... 
num_matches: 32, 
view_instance: properties_in_view: [Object], 
[ { view_instance_ctr: 0, view_id: '4e5bb37258200ed9aa000011' }, 
... 

目標是iOS的5.0,所以我假設它使用NSJSONSerialization創建JSON(雖然我沒有試過尚未驗證)。我發送的字典使用isValidJSONObject驗證爲JSON。如果我打印出序列化版本,它看起來很棒。代碼的簡化版本是這樣的:

NSDictionary *params = myDictionaryThatValidatesToJSON; 
httpClient.parameterEncoding = AFJSONParameterEncoding; 
NSMutableURLRequest *request = [httpClient 
    requestWithMethod:@"POST" path:@"" parameters:params]; 

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; 
[operation start]; 

我希望有一個bigDummy = NO標誌我失蹤。

回答

0

我認爲你的問題是在服務器端 - 即。您在問題中引用的調試不是服務器接收到的原始JSON文本,但重新解釋了服務器上的某個組件已完成的這一操作。

+0

我敢肯定,這是在服務器上正在接收什麼但我會仔細檢查。請注意,我可以使用Firefox REST插件成功發送相同的JSON,並且實際上記錄的數據都是雙引號。還有一點需要注意:以前我已經成功地將其他數據發送到服務器上的其他POST API,但是我總是使用一個大字符串初始化一個字典,我創建了一個轉義雙引號。我現在會嘗試一下,看看它是否有所作爲,並確保它不能成爲服務器問題。謝謝。 – michael 2012-03-05 16:31:08

+0

另一種選擇是,如果JSON序列化程序確實生成[Object],那麼您的字典中必須有一個不尋常的對象 – JosephH 2012-03-05 16:45:21

+1

我不太瞭解AFNetworking,儘管它說它將使用NSJSONSerialization(如果可用)。如果我這樣做: NSData * jsonData = [NSJSONSerialization dataWithJSONObject:outDict options:NSJSONWritingPrettyPrinted error:&error]; NSString * jsonOut = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; 它生成有效的JSON。我以編程方式創建JSON,它非常簡單,所以我不認爲我介紹了任何奇怪的東西。 – michael 2012-03-05 17:17:47

0

邁克爾是正確的。通過使用他的數據的代碼,我用這與JSON參數進行POST請求:

//數據字典是你的參數字典

NSError *error = nil; 

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

    //NSString *jsonOut = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; 

    AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:webURL]]; 

    [httpClient setParameterEncoding:AFFormURLParameterEncoding]; 

    NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST" 
                  path:@"Webservice URL" 
                 parameters:nil]; 

    NSMutableData *body = [NSMutableData data]; 

    [body appendData:jsonData]; 

    [request setHTTPBody:body]; 
    [request setHTTPMethod:@"POST"]; 
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; 
    [request setValue:@"application/json" forHTTPHeaderField:@"content-type"]; 

    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; 

    [httpClient registerHTTPOperationClass:[AFHTTPRequestOperation class]]; 

    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { 

     // Print the response body in text 

     NSLog(@"Response: %@", [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]); 


    } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 

     NSLog(@"Error: %@", error); 


    }]; 
    [operation start]; 
+1

您也可以使用'AFHTTPClient'的'setParameterEncoding',而不必設置其中一些標頭。 – 2013-02-27 15:05:25

+0

@KeithSmiley:是的你是對的。 – Muzammil 2013-03-05 13:25:55