2014-01-27 22 views
0
軌服務器

我已經通過設置內容類型爲application/JSON和此JSON代碼成功地使用HTTP客戶端做一個帖子:HTTP後W/JSON從iOS的

{ 
    "order": { 
     "name": "Tia Carter", 
     "location": "Corams", 
     "phone_number": "707", 
     "food": "Bobcat Burger" 
    } 
} 

代碼工作完美該訂單已在數據庫中註冊。我正在嘗試將其應用到我的iOS應用程序中,但不斷收到關於json中冒號的語法錯誤。這是客觀C代碼:

NSURL *nsURL = [[NSURL alloc] initWithString:@"http://0.0.0.0:3000/orders.json"]; 
NSMutableURLRequest *nsMutableURLRequest = [[NSMutableURLRequest alloc] initWithURL:nsURL]; 

// Set the request's content type to application/x-www-form-urlencoded 
[nsMutableURLRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 

// Set HTTP method to POST 
[nsMutableURLRequest setHTTPMethod:@"POST"]; 

// Set up the parameters to send. 
NSString *paramDataString = [NSString stringWithFormat:@ 
          "{ "order": {"%@:" ="%@","%@:"="%@","%@:"="%@","%@:"="%@"}}", @"name", _name.text, @"location", _location.text, @"phone_number", _phoneNumber.text, @"food", _order.text]; 

// Encode the parameters to default for NSMutableURLRequest. 
NSData *paramData = [paramDataString dataUsingEncoding:NSUTF8StringEncoding]; 

// Set the NSMutableURLRequest body data. 
[nsMutableURLRequest setHTTPBody: paramData]; 

// Create NSURLConnection and start the request. 
NSURLConnection *nsUrlConnection=[[NSURLConnection alloc]initWithRequest:nsMutableURLRequest delegate:self]; 

我會很感激任何想法或指導。謝謝。

回答

0

我相信你有兩個問題:

  • 你沒有逃脫配額(所有的人把前\)
  • 你並不需要把文字「姓名」,「位置」和(這本身不是問題,只是一種風格的東西)

此外,我會建議使用NSDictionary,並在需要時將其轉換爲JSON(它會爲您節省很多神經)未轉義的配額,丟失的支架等)。

看看這個問題如何NSDictionary中轉換成JSON:

Generate JSON string from NSDictionary in iOS