2011-06-28 75 views
4

我試圖模仿他們使用的是硬編碼的JSON什麼是生成從C#

{ 
"page": 1, 
"total": 1, 
"records": 2, 
"rows": [ 
    {"id": 1, "cell": ["1", "Super Item", "300", 0, null, false, false]}, 
     {"id": 2, "cell": ["2", "Item 1", "100", 1, 1, false, false]}, 
     {"id": 3, "cell": ["3", "Sub Item 1", "50", 2, 2, true, true]}, 
     {"id": 4, "cell": ["4", "Sub Item 2", "25", 2, 2, false, false]}, 
     {"id": 5, "cell": ["5", "Sub-sub Item 1", "25", 3, 4, true, true]}, 
     {"id": 6, "cell": ["6", "Sub Item 3", "25", 2, 2, true, true]}, 
     {"id": 7, "cell": ["7", "Item 2", "200", 1, 1, false, false]}, 
     {"id": 8, "cell": ["8", "Sub Item 1", "100", 2, 7, false, false]}, 
     {"id": 9, "cell": ["9", "Sub-sub Item 1", "50", 3, 8, true, true]}, 
     {"id": 10, "cell": ["10", "Sub-sub Item 2", "50", 3, 8, true, true]}, 
     {"id": 11, "cell": ["11", "Sub Item 2", "100", 2, 7, true, true]} 
    ] 
} 

一個例子,但我需要從C#生成此JSON的最佳途徑。有沒有關於在C#中生成上述內容的最佳方法的建議?

+2

'DataContractJsonSerializer'(內置)或Json.net – CodesInChaos

+0

在我看來,答案取決於更多的信息。例如,您是否使用ASP.NET MVC,WFC或ASMX Web服務。你使用哪個.NET版本? – Oleg

+0

@Oleg - 這是爲MVC,因爲我試圖與jqgrid Treegrid玩耍。你有任何treegrid和asp.net-mvc的例子(鑑於你是專家:)) – leora

回答

11

Controller類有一個Json方法將對象序列化爲JSON,因此在您的操作方法中,您只需創建對象並調用該方法:

public ActionResult GetData() { 
    return Json(
    new { 
     page = 1, 
     total = 1, 
     records = 2, 
     rows = new[] { 
     new { id = 1, cell = new object[] { "1", "Super Item", "300", 0, null, false, false } }, 
     new { id = 2, cell = new object[] { "2", "Item 1", "100", 1, 1, false, false } }, 
     new { id = 3, cell = new object[] { "3", "Sub Item 1", "50", 2, 2, true, true } }, 
     new { id = 4, cell = new object[] { "4", "Sub Item 2", "25", 2, 2, false, false } }, 
     new { id = 5, cell = new object[] { "5", "Sub-sub Item 1", "25", 3, 4, true, true } }, 
     new { id = 6, cell = new object[] { "6", "Sub Item 3", "25", 2, 2, true, true } }, 
     new { id = 7, cell = new object[] { "7", "Item 2", "200", 1, 1, false, false } }, 
     new { id = 8, cell = new object[] { "8", "Sub Item 1", "100", 2, 7, false, false } }, 
     new { id = 9, cell = new object[] { "9", "Sub-sub Item 1", "50", 3, 8, true, true } }, 
     new { id = 10, cell = new object[] { "10", "Sub-sub Item 2", "50", 3, 8, true, true } }, 
     new { id = 11, cell = new object[] { "11", "Sub Item 2", "100", 2, 7, true, true } } 
     } 
    } 
); 
} 
+0

+!如果創建可序列化的類是過度殺傷性的,那麼非常適合一次性事情。也最接近JS等價物。 – Davy8

0
class Row { 
    public int id {get;set;} 
    public object[] cell {get;set;} 
} 

class Data { 
    public int page {get;set;} 
    public int total {get;set;} 
    public int records {get;set;} 
    public Row[] rows {get;set;} 
} 

var myData = new Data(){ .... }; 
var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(myData); 
+0

這些類是不可序列化的 – harryovers

+0

他們不需要可序列化。因爲這不是使用二進制序列化或json數據合同。它使用Json序列化,它不處理可序列化屬性 – rpgmaker

+0

你的權利,我沒有發現:) – harryovers

4

有一個內置到.Net 2+中的類,它被稱爲'JavaScriptSerializer',它創建基於.Net類型類的JSON結構化字符串。

使用序列化程序,您可以簡單地創建一個具有屬性和集合的類來表示您的JSON數據。在.Net服務器端代碼中創建一個實例,然後使用序列化器響應以生成有效的JSON字符串響應。

下面是將Person類實例轉換爲序列化的JSON字符串的示例;

JavaScriptSerializer js = new JavaScriptSerializer(); 
Person p1 = new Person(); 
p1.firstName = "Brian"; 
p1.lastName = "Scott"; 
p1.department = "Microsoft"; 
p1.address.addressline1 = "Microsoft"; 
p1.address.addressline2 = ""; 
p1.address.city = "Redmond"; 
p1.address.state = "Seattle"; 
p1.address.country = "America"; 
p1.address.pin = 560028; 
p1.technologies = new string[] { "IIS", "ASP.NET", "JavaScript", "AJAX" }; 

string strJSON = js.Serialize(p1); 

這將產生

{"firstName":"Brian","lastName":"Scott","department":"Microsoft","address":{"addressline1":"Microsoft","addressline2":"","city":"Redmond","state":"Seattle","country":"America","pin":560028},"technologies":["IIS","ASP.NET","JavaScript","AJAX"]} 

有效的JSON字符串如果您打算使用Web服務來產生JSON響應到客戶端,那麼你可以爲你的標記方法;

[WebMethod] 
[ScriptMethod(ResponseFormat = ResponseFormat.Json)] 
public string GetPersonJSON() 
{ 
    JavaScriptSerializer js = new JavaScriptSerializer(); 
    Person p1 = new Person(); 
p1.firstName = "Brian"; 
p1.lastName = "Scott"; 
p1.department = "Microsoft"; 
p1.address.addressline1 = "Microsoft"; 
p1.address.addressline2 = ""; 
p1.address.city = "Redmond"; 
p1.address.state = "Seattle"; 
p1.address.country = "America"; 
p1.address.pin = 560028; 
p1.technologies = new string[] { "IIS", "ASP.NET", "JavaScript", "AJAX" }; 

return js.Serialize(p1); 
} 
+1

3.5+,似乎。但是,仍然不需要外部廢話。 – cHao

+1

謝謝,我認爲這是一個包含2.0+的內容,但現在我想到了這個類確實伴隨着3.0的其他System.web.extension類。乾杯。 –

1

顯然你正試圖填充一個jqGrid,而你正在使用ASP.NET MVC。 如果您已經爲這些值定義的類:

["1", "Super Item", "300", 0, null, false, false] 

可以將所有元素存儲在一個集合中myCollection
你可以做這樣的事情:

var ReturnData = new 
    { 
     total = totalPages, 
     page = page, 
     records = totalRecords, 
     rows = myCollection.Select(r => new 
     { 
      id = r.Id.ToString(), 
      cell = new String[] { r.Field1, r.Field2, r.Field3, r.Field4 } 
     }) 
     }; 

return (Json(ReturnData, JsonRequestBehavior.DenyGet));