2017-08-15 42 views
0

繼DataTable中的數據(「|」就是各自的COL3匹配COL4/COL5分隔符):如何正確創建的Web API一個JSON文件

Col1  Col2  Col3   Col4        Col5 
John  Doe   12    156-345        792-098 
Mike  Keller  12|15   145-394|909-203      156-323|121-444,134-232 
Hanes  Wara  34|12|18  180-655,202-175|123-654|118-000  121-343|654-222|109-220 

我現在有這只是顯示一個自定義列表在API調用這樣的數據:

[HttpGet] 
public System.Web.Mvc.JsonResult RTData() 
{ 
    //...call SQL to retrieve the data and populate the {custom List} 
    //return... 
    return new System.Web.Mvc.JsonResult { Data = {custom List} }; 
} 

這是我得到的(在瀏覽器控制檯)當我打電話從JQuery的API的是什麼:

Object 
    ContentEncoding:null 
    ContentType:null 
    Data:Array(12) 
     [0 … 11] 
     length:12 
     __proto__:Array(0) 
    JsonRequestBehavior:1 
    MaxJsonLength:null 
    RecursionLimit:null 
    __proto__:Object 

每個結果的數據是這樣的:

Data:Array(12) 
    [0 … 11] 
    0: 
     Col1: "John" 
     Col2: "Doe" 
     Col3: "12" 
     Col4: "156-345" 
     Col5: "792-098" 
    1: 
     Col1: "Mike" 
     Col2: "Keller" 
     Col3: "12|15" 
     Col4: "145-394|909-203 " 
     Col5: "156-323|121-444,134-232" 

在JSON所需的輸出:

{ 
    "Col1": "John", 
    "Col2": "Doe", 
    "Col3Combined": [ 
     { 
      "Col3": "12", 
      "Col4Combined": [ 
       { 
        "Col4": "156-345" 
       } 
      ], 
      "Col5Combined": [ 
       { 
        "Col5": "792-098" 
       } 
      ] 
     } 
    ] 
}, 
{ 
    "Col1": "Mike", 
    "Col2": "Keller", 
    "Col3Combined": [ 
     { 
      "Col3": "12", 
      "Col4Combined": [ 
       { 
        "Col4": "145-394" 
       } 
      ], 
      "Col5Combined": [ 
       { 
        "Col5": "156-323" 
       } 
      ] 
     }, 
     { 
      "Col3": "15", 
      "Col4Combined": [ 
       { 
        "Col4": "909-203" 
       } 
      ], 
      "Col5Combined": [ 
       { 
        "Col5": "121-444", 
        "Col5": "134-232" 
       } 
      ] 
     } 
    ] 
}...//more data 

如何能夠做到在API調用JSON格式?

類我想出了哪些應該足夠?

public class RootObject 
{ 
    public string col1 { get; set; } 
    public string col2 { get; set; } 
    public List<col3data> col3 { get; set; } 
} 

public class col3data 
{ 
    public string col3d { get; set; } 
    public List<col4data> col4d { get; set; } 
    public List<col5data> col5d { get; set; } 
} 

public class col4data 
{ 
    public string col4 { get; set; } //since col4 can also have comma separated values within each entry, as seen for Hanes, should this be a list too? 
} 

public class col5data 
{ 
    public string col5 { get; set; } //since col5 can also have comma separated values within each entry, as seen for Mike, should this be a list too? 
} 

像這樣的東西也:

public class RootObject 
{ 
    public string col1 { get; set; } 
    public string col2 { get; set; } 
    public List<col3data> col3 { get; set; } 
} 

public class col3data 
{ 
    public string col3d { get; set; } 
    public List<col4data> col4d { get; set; } 
    public List<col5data> col5d { get; set; } 
} 

public class col4data 
{ 
    public List<col4subdata> col4sub { get; set; } 
} 

public class col4subdata 
{ 
    public string col4_1 { get; set; } 
    public string col4_2 { get; set; } 
} 

public class col5data 
{ 
    public List<col5subdata> col5sub { get; set; } 
} 

public class col5subdata 
{ 
    public string col5_1 { get; set; } 
    public string col5_2 { get; set; } 
} 

我想現在我會通過每一行進行迭代,並添加到RootObject類,並從它創建一個JSON。我可以得到一些幫助嗎?

+1

請說明你是如何在瀏覽器控制檯中獲得輸出的。 'console.log(JSON.stringify(data));'(或類似取決於你的變量名)應該顯示爲JSON。您還可以在瀏覽器的網絡選項卡中確切地查看服務器的原始響應。如果你直接將JS對象直接登錄到控制檯而不進行字符串化處理(請記住,你的代碼可能會自動將JSON字符串轉換爲JS對象) – ADyson

+0

您使用的是什麼版本的Web API? Web API 2或Core? –

+0

Web API 2 ...只是爲了澄清,我只是想請求幫助從主鍵創建子鍵並迭代它。 – Si8

回答

1

您需要使用JavascriptSerializer。

JavaScriptSerializer js = new JavaScriptSerializer(); 
Context.Response.Write(js.Serialize(lstCustom)); 

在當你調用API jQuery函數,如果使用CONSOLE.LOG(response.data),你應該看到JSON格式的自定義列表。

您將不得不創建一個具有與結果集中的列匹配的屬性的類。

class Custom 
{ 
    public string Col1 {get; set;} 
    .... 
    public List<int> Col3 {get; set;} 
    .... 
} 

然後遍歷設置爲構建自定義列表對象(lstCustom)在結果中的列。像上面那樣使用js.Serialize()。

+0

感謝您的回覆。注意數據是如何進來的,我必須把它們分開。當我使用JSSerializer的時候,我在我的JSON周圍獲得''' – Si8

+0

Web API將數據串行化爲JSON,默認情況下已經使用JSON.NET – ADyson

+0

但是列數據位於不同的位置,所以邏輯如何工作? – Si8

0

我認爲問題在於您的列表未被序列化,而contenttype爲空,因此瀏覽器不確定如何處理它。看到這個答案,希望這也可以幫助你:Return a JSON string explicitly from Asp.net WEBAPI?

+0

一切正在爲我工​​作,但我想創建嵌套的鍵,你可以從期望的結果看到。 – Si8

+1

好涼快。正如其他人現在所說,你的嵌套鍵需要在你的對象上作爲集合類型(List等)。一旦你序列化該對象並返回序列化的JSON對象,它將正確地將它們嵌套。 – Jacques