2011-06-01 86 views
0

我見過很多方法在C#中輸出JSON。然而,他們中的任何一個都不容易。這可能是由於我缺乏C#知識所致。在C#中的JSON(最好沒有第三方工具)

問題:如果您要編寫JSON(稍後在JavaScript中使用),您將如何執行此操作?我寧願不使用第三方工具;然而,這不是任何形式的最後通。。 JSON可以在C#ASP.NET中使用嗎? JSON的

例子我希望寫:

{ 
    "Trips": [ 
     { 
      "from": "here", 
      "to": "there", 
      "stops": [ 
       { 
        "place": "middle1", 
        "KMs": 37 
       }, 
       { 
        "place": "middle2", 
        "KMs": 54 
       } 
      ] 
     }, 
     { 
      "from": "there", 
      "to": "here", 
      "stops": [ 
       { 
        "place": "middle2", 
        "KMs": 37 
       }, 
       { 
        "place": "middle1", 
        "KMs": 54 
       } 
      ] 
     } 
    ] 
} 
+0

你是不是想序列化對象,寫從網站一些JSON或簡單地保持一些數據以JSON格式存儲到文件中?您需要更具體才能獲得體面的答案。 – 2011-06-01 17:27:19

+0

[泛型/ JSON JavaScriptSerializer C#]可能的重複(http://stackoverflow.com/questions/304081/generics-json-javascriptserializer-c) – manojlds 2011-06-01 17:30:56

+0

重複的[this](http://stackoverflow.com/questions/6201529導通/ C-對象到-A-JSON串式淨4)? – goalie7960 2011-06-01 17:28:53

回答

3

怎麼樣JavaScriptSerializer?好的集成解決方案,允許用JavaScriptConverter s擴展。


演示的你是什麼後:

using System; 
using System.Web.Script.Serialization; 
using System.Text; 

public class Stop 
{ 
    public String place { get; set; } 
    public Int32 KMs { get; set; } 
} 

public class Trip 
{ 
    public String from { get; set; } 
    public String to { get; set; } 
    public Stop[] stops { get; set; } 
} 

public class MyJSONObject 
{ 
    public Trip[] Trips { get; set; } 

    public override String ToString() 
    { 
     StringBuilder sb = new StringBuilder(); 
     foreach (Trip trip in this.Trips) 
     { 
      sb.AppendFormat("Trip:\r\n"); 
      sb.AppendFormat("\t To: {0}\r\n", trip.to); 
      sb.AppendFormat("\t From: {0}\r\n", trip.from); 
      sb.AppendFormat("\tStops:\r\n"); 
      foreach (Stop stop in trip.stops) 
      { 
       sb.AppendFormat("\t\tPlace: {0}\r\n", stop.place); 
       sb.AppendFormat("\t\t KMs: {0}\r\n", stop.KMs); 
      } 
      sb.AppendLine(); 
     } 
     return sb.ToString(); 
    } 
} 
public class Test 
{ 
    public static void Main() 
    { 
     String example = "{\"Trips\":[{\"from\":\"here\",\"to\":\"there\",\"stops\":[{\"place\":\"middle1\",\"KMs\":37},{\"place\":\"middle2\",\"KMs\":54}]},{\"from\":\"there\",\"to\":\"here\",\"stops\":[{\"place\":\"middle2\",\"KMs\":37},{\"place\":\"middle1\",\"KMs\":54}]}]}"; 

     JavaScriptSerializer serializer = new JavaScriptSerializer(); 

     // Parse the string to our custom object 
     MyJSONObject result = serializer.Deserialize<MyJSONObject>(example); 
     Console.WriteLine("Object deserialized:\r\n\r\n{0}\r\n\r\n", result); 

     // take our custom object and dump it in to a string 
     StringBuilder sb = new StringBuilder(); 
     serializer.Serialize(result, sb); 
     Console.WriteLine("Object re-serialized:\r\n\r\n{0}\r\n\r\n", sb); 

     // check step 
     Console.WriteLine(example.Equals(sb.ToString()) ? "MATCH" : "NO match"); 
    } 
} 

和輸出:

Object deserialized: 

Trip: 
      To: there 
     From: here 
     Stops: 
       Place: middle1 
        KMs: 37 
       Place: middle2 
        KMs: 54 

Trip: 
      To: here 
     From: there 
     Stops: 
       Place: middle2 
        KMs: 37 
       Place: middle1 
        KMs: 54 




Object re-serialized: 

{"Trips":[{"from":"here","to":"there","stops":[{"place":"middle1","KMs":37},{"pl 
ace":"middle2","KMs":54}]},{"from":"there","to":"here","stops":[{"place":"middle 
2","KMs":37},{"place":"middle1","KMs":54}]}]} 

MATCH 
Press any key to continue . . . 
+0

這看起來像我需要的確切的東西。我不知道我是如何錯過它的!非常感謝。 – Hejner 2011-06-01 17:31:14

+0

@Hejner:如果您有興趣,我已經爲您添加了演示。 – 2011-06-01 17:50:39

1

你真的應該看看Json.NET

這不僅寄望庫執行它可以解析JSON也是如此。這意味着你可以輕鬆地往返東西並通過JSON進行通信而不涉及WCF服務層。它還可以做其他精巧的事情,例如提供可查詢的動態對象模型。

+1

我同意。爲什麼要發明輪子? – tofutim 2011-06-01 17:28:01

相關問題