2013-02-12 62 views
5

我是json格式的新手。我想通過使用C#和json.net包創建一個後續的json字符串。如何使用C#創建我的json字符串?

這是我的目標JSON格式:

{ 
    "GetQuestions": 
    { 
    "s1":"Q1,Q2", 
    "s2":"Q1,Q3", 
    "s3":"Q4,Q5" 
    } 
} 

在這裏,我存儲每個學生有時questions.But,學生的總數目可以是例如vary.for可以S1,S2僅或S1 ,s2,s3,s4 ....這是在C#運行時計算的。所以,我想根據學生列表創建json字符串....

請指導我擺脫這個問題?

+0

[你嘗試過什麼(http://whathaveyoutried.com)?你的名單是怎樣的? – 2013-02-12 08:41:59

+0

[Json.NET](http://json.net/) – 2013-02-12 08:43:53

回答

13

的JSON是一種奇怪的,它就像學生是「GetQuestion」對象的屬性,它應該很容易成爲一個列表.....

關於你可以使用的庫是。

而且可能有更多的人,但是這是我所使用

關於JSON我不現在可能是這樣的

public class GetQuestions 
{ 
    public List<Student> Questions { get; set; } 
} 

public class Student 
{ 
    public string Code { get; set; } 
    public string Questions { get; set; } 
} 

void Main() 
{ 
    var gq = new GetQuestions 
    { 
     Questions = new List<Student> 
     { 
      new Student {Code = "s1", Questions = "Q1,Q2"}, 
      new Student {Code = "s2", Questions = "Q1,Q2,Q3"}, 
      new Student {Code = "s3", Questions = "Q1,Q2,Q4"}, 
      new Student {Code = "s4", Questions = "Q1,Q2,Q5"}, 
     } 
    }; 

    //Using Newtonsoft.json. Dump is an extension method of [Linqpad][4] 
    JsonConvert.SerializeObject(gq).Dump(); 
} 

Linqpad

,結果是這樣的

{ 
    "Questions":[ 
     {"Code":"s1","Questions":"Q1,Q2"}, 
     {"Code":"s2","Questions":"Q1,Q2,Q3"}, 
     {"Code":"s3","Questions":"Q1,Q2,Q4"}, 
     {"Code":"s4","Questions":"Q1,Q2,Q5"} 
     ] 
} 

是的,我知道了JSON是不同的,但要與字典中的JSON。

void Main() 
{ 
    var f = new Foo 
    { 
     GetQuestions = new Dictionary<string, string> 
       { 
        {"s1", "Q1,Q2"}, 
        {"s2", "Q1,Q2,Q3"}, 
        {"s3", "Q1,Q2,Q4"}, 
        {"s4", "Q1,Q2,Q4,Q6"}, 
       } 
    }; 

    JsonConvert.SerializeObject(f).Dump(); 
} 

class Foo 
{ 
    public Dictionary<string, string> GetQuestions { get; set; } 
} 

並與字典是你想要的吧.....

{ 
     "GetQuestions": 
     { 
       "s1":"Q1,Q2", 
       "s2":"Q1,Q2,Q3", 
       "s3":"Q1,Q2,Q4", 
       "s4":"Q1,Q2,Q4,Q6" 
     } 
} 
+0

非常感謝。我在GetQuestions Dictionary中添加了這些信息(使用Add方法)。實際上,格式稍有改變。 { \t GetQuestions: \t \t { \t \t \t 「S1」: 「Q1,Q2」, \t \t \t 「S2」: 「Q3,Q4」, \t \t \t 「S3」:「Q5,Q6 」 \t \t} \t \t] }能否請你幫我在這... – Saravanan 2013-02-16 06:29:28

+1

偉大的答案!但是,我對'轉儲'感到困惑。我挖了一些挖,發現你可以這樣做:'JsonConvert.SerializeObject(gq,Formatting.Indented);'並且它不需要任何瘋狂。 – 2015-04-30 10:24:50

+0

如何使用循環添加多個元素?我有來自用戶的多個輸入。你能給我建議嗎? – Bhat 2017-02-12 12:19:08

11

沒有真正需要JSON.NET包。你可以使用JavaScriptSerializerSerialize method將把託管類型的實例變成JSON字符串。

var serializer = new JavaScriptSerializer(); 
var json = serializer.Serialize(instanceOfThing); 
6

要將任何對象或對象列表轉換爲JSON,我們必須使用函數JsonConvert.SerializeObject。

下面的代碼演示了在ASP.NET環境中使用JSON的:

using System; 
using System.Data; 
using System.Configuration; 
using System.Collections; 
using System.Web; 
using System.Web.Security; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
using System.Web.UI.HtmlControls; 
using Newtonsoft.Json; 
using System.Collections.Generic; 

namespace JSONFromCS 
{ 
    public partial class _Default : System.Web.UI.Page 
    { 
     protected void Page_Load(object sender, EventArgs e1) 
     { 
      List<Employee> eList = new List<Employee>(); 
      Employee e = new Employee(); 
      e.Name = "Minal"; 
      e.Age = 24; 

      eList.Add(e); 

      e = new Employee(); 
      e.Name = "Santosh"; 
      e.Age = 24; 

      eList.Add(e); 

      string ans = JsonConvert.SerializeObject(eList, Formatting.Indented); 

      string script = "var employeeList = {\"Employee\": " + ans+"};"; 
      script += "for(i = 0;i<employeeList.Employee.length;i++)"; 
      script += "{"; 
      script += "alert ('Name : ='+employeeList.Employee[i].Name+' 
      Age : = '+employeeList.Employee[i].Age);"; 
      script += "}"; 

      ClientScriptManager cs = Page.ClientScript; 
      cs.RegisterStartupScript(Page.GetType(), "JSON", script, true); 
     } 
    } 
    public class Employee 
    { 
     public string Name; 
     public int Age; 
    } 
} 

運行該程序後,你會得到兩個警報

在上面的例子中,我們創建了一個列表Employee對象並將其傳遞給函數「JsonConvert.SerializeObject」。這個函數(JSON庫)將把對象列表轉換成JSON格式。 JSON的實際格式可以在下面的代碼段中查看:

{ "Maths" : [ {"Name"  : "Minal",  // First element 
          "Marks"  : 84, 
          "age"  : 23 }, 
          { 
          "Name"  : "Santosh", // Second element 
          "Marks"  : 91, 
          "age"  : 24 } 
          ],      
       "Science" : [ 
          { 
          "Name"  : "Sahoo",  // First Element 
          "Marks"  : 74, 
          "age"  : 27 }, 
          {       
          "Name"  : "Santosh", // Second Element 
          "Marks"  : 78, 
          "age"  : 41 } 
          ] 
      } 

語法:

  • {} - 充當 '集裝箱'

  • [] - 保持陣列

  • : - 名稱和值用冒號分開

  • , - 數組元素之間用逗號

此代碼是爲中級程序員,誰想要使用C#2.0創建JSON和ASPX頁面使用分離。

您可以從JavaScript端創建JSON,但您會如何將對象列表轉換爲C#中等效的JSON字符串。這就是爲什麼我寫這篇文章。

在C#3.5中,有一個內置類用於創建名爲JavaScriptSerializer的JSON。

以下代碼演示瞭如何使用該類在C#3.5中將其轉換爲JSON。

JavaScriptSerializer serializer = new JavaScriptSerializer() 
return serializer.Serialize(YOURLIST); 

所以,嘗試創建與問題陣列的列表,然後序列化此列表分爲JSON

+4

專業提示:針對示例演示代碼,定位控制檯窗口或單元測試。不是一個網頁。讓它 - 容易 - 讓他們測試/嘗試,你的答案。 – 2013-02-12 08:55:19

相關問題