2011-06-01 87 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

相关问题