2017-10-10 53 views
2

我需要發佈對象中的數組API,這個API是這樣的:發送對象中的JSON數組到API

{ 
    "ds_seatInfo": [ 
    { 
     "SEAT_LOC_NO": "00201901", 
     "SEAT_LOC_NO": "00201902" 
    } 
    ], 
    "SCN_SCH_SEQ": "13178", 
    "REQ_FG_CD": "01", 
    "LOCK_APRV_KEY": "123123" 
} 

使用定義如下模型我已嘗試:

public class ds_seatInfo 
    { 
     public List<string> SEAT_LOC_NO { get; set; } 
    } 

public class BookParam 
    { 
     public string SCN_SCH_SEQ { get; set; } 
     public ds_seatInfo ds_seatInfo { get; set; } 
     public string REQ_FG_CD { get; set; } 
     public string LOCK_APRV_KEY { get; set; } 
    } 

但結果與預期不符,即模型返回:

"{\"SCN_SCH_SEQ\":\"13178\",\"ds_seatInfo\":{\"SEAT_LOC_NO\":[\"00201901\",\"00201902\"]},\"REQ_FG_CD\":\"01\",\"LOCK_APRV_KEY\":\"123123\"}" 

這意味着SEAT_LOC_NO未按預期方式讀取。我正在使用Newtonsoft進行序列化模型。

我該怎麼辦?

+0

WebApi操作的方法簽名是什麼? – DiskJunky

+0

@DiskJunky HTTPPost – blacoffees

+0

我的意思是如何聲明,例如,'公共無效DoSomething(SomeObject param1,...)' – DiskJunky

回答

0

沒有測試它,但可能這會幫助你或者讓你在正確的方向:

public class BookParam 
{ 
    [JsonProperty("ds_seatInfo")] 
    public List<KeyValuePair<string, string>> SetInfos = new List<KeyValuePair<string, string>>(); 

    [JsonProperty("SCN_SCH_SEQ")] 
    public string ScnSchSeq { get; set; } 

    [JsonProperty("REQ_FG_CD")] 
    public string ReqFgCd { get; set; } 

    [JsonProperty("LOCK_APRV_KEY")] 
    public string LockAprvKey { get; set; } 
} 

而當你將項目添加到SetInfos嘗試這樣的:

SetInfos.Add(new KeyValuePair<string, string>("SEAT_LOC_NO", "00201901")); 

編輯

另一種可能的實現

public class BookParam 
{ 
    [JsonProperty("ds_seatInfo")] 
    public List<SeatInfo> DsSeatInfo = new List<SeatInfo>(); 

    [JsonProperty("SCN_SCH_SEQ")] 
    public string ScnSchSeq { get; set; } 

    [JsonProperty("REQ_FG_CD")] 
    public string ReqFgCd { get; set; } 

    [JsonProperty("LOCK_APRV_KEY")] 
    public string LockAprvKey { get; set; } 
} 

public class SeatInfo() 
{ 
    [JsonProperty("SEAT_LOC_NO")] 
    public string SeatLocNo { get; set; } 
} 
+0

謝謝你的隊友,這個解決了我的問題。儘管它也發送了'Key'和'Value'對象。 – blacoffees

+0

沒問題,我用另一種方式添加了一個編輯,這種方式也可能適用於您,並且不會有關鍵和價值問題。 – Isma