C#中的JSON(最好没有第三方工具)

时间:2011-06-01 17:25:07

标签: asp.net json c#-4.0

我已经看到了很多在C#中输出JSON的方法。然而,它们中没有一个看起来容易。这很可能是由于我对C#缺乏了解。

问题:如果你要编写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
                }
            ]
        }
    ]
}

3 个答案:

答案 0 :(得分:3)

JavaScriptSerializer怎么样?很好的集成解决方案,允许使用JavaScriptConverter进行扩展。


演示你的目标:

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 . . .

答案 1 :(得分:2)

System.Web.Extensions.dll(.NET 3.5 SP1)中有JavaScriptSerializer

http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx

答案 2 :(得分:1)

你应该看看Json.NET

这个库不仅表现出色,它还可以解析JSON。这意味着您可以轻松地往返行程并通过JSON进行通信,而无需涉及WCF服务层。它还做其他整洁的事情,例如提供可查询的动态对象模型。