2016-08-11 8 views
0

私のサーバーは、アプリケーション側から辞書の配列を期待している問題で実行しています。私はtravel_configsalamafire(Postメソッド)を使用して迅速に辞書の配列を送信する方法

{ 
let param : [String : AnyObject] = 
      ["trf_id" : Constant.constantVariables.trfID, 
      "mode"    : Constant.modeValues.createMode, 
      "to_city"   : Constant.constantVariables.to_city, 
      "from_city"   : Constant.constantVariables.from_city, 
      "description"  : Constant.constantVariables.descrption, 
      "request_type"  : Constant.constantVariables.request_type, 
      "to_date"   : Constant.constantVariables.to_date!, 
      "from_date"   : Constant.constantVariables.from_date!, 
      "travel_configs" : ["Config_id" : "9","values" : "train", 
             "Config_id" : "10","values" : "bus"]] 
     print(param) 
} 
  • の下に、ここで間違ったコードを行くよどこ示唆してください、私はので、サーバーの例外をこのようにそれを送信する必要が辞書

の配列です

trf_id:37 
mode:1 
request_type:0 
from_city:sdfsd 
to_city:qws 
from_date:2016-08-17 
to_date:2016-08-26 
description:sdfsdf 
travel_configs:[ 
{"config_id":"11","values":"1"} 
,{"config_id":"2","values":"Flight"}] 
+1

travel_configsは辞書です。 ["Config_id": "9"、 "values": "train"]、["Config_id": "10"、 "values": "bus"]]は辞書の配列になります。 –

+0

@ ShadowOfはい辞書の配列を作成していますが、私のサーバーはその辞書を受け入れていないので、私はサーバーから期待通りに送る必要があります。 –

+0

@ ShadowOfそして、それを送信しているパラメータがnullになっています。 –

答えて

0

は最終的に私はちょうど(JSONStringify FUNCこの クラスに辞書のあなたの配列を送る

を苦労の3日後に答えを得ました値:ANYOBJECT、prettyPrinted:ブール=偽) - >文字列{

let options = prettyPrinted ? NSJSONWritingOptions.PrettyPrinted : NSJSONWritingOptions(rawValue: 0) 


    if NSJSONSerialization.isValidJSONObject(value) { 

     do{ 
      let data = try NSJSONSerialization.dataWithJSONObject(value, options: options) 
      if let string = NSString(data: data, encoding: NSUTF8StringEncoding) { 
       return string as String 
      } 
     }catch { 

      print("error") 
      //Access error here 
     } 

    } 
    return "" 

} 

これは他のいくつかのいずれかをありがとう役立つことを願っています。..

-1

まず、 "config_id"の "c"ではなく "C"という大文字から来るかもしれませんか?

セカンダリ、あなたが実際に不足している[]、あなたのコードは次のようになります。

let param : [String : AnyObject] = 
      ["trf_id" : Constant.constantVariables.trfID, 
      "mode"    : Constant.modeValues.createMode, 
      "to_city"   : Constant.constantVariables.to_city, 
      "from_city"   : Constant.constantVariables.from_city, 
      "description"  : Constant.constantVariables.descrption, 
      "request_type"  : Constant.constantVariables.request_type, 
      "to_date"   : Constant.constantVariables.to_date!, 
      "from_date"   : Constant.constantVariables.from_date!, 
      "travel_configs" : [["Config_id" : "9","values" : "train", 
             "Config_id" : "10","values" : "bus"]]] 

なぜ?サーバーが辞書の配列を期待している、とあなただけの辞書を送信しているので ;)

+0

Broそれは正しい方法ではない私はそれをチェックした –

+0

はいjsonのフォーマットを使用しています –

+0

私の答えを編集しました;) – RomOne

関連する問題