【问题标题】:JSON in C# (preferably without third-party tools)C# 中的 JSON(最好没有第三方工具)
【发布时间】:2011-09-06 12:12:22
【问题描述】:

我见过很多在 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
                }
            ]
        }
    ]
}

【问题讨论】:

  • 您是在尝试序列化对象、从网站写入一些 json,还是只是将一些数据以 JSON 格式保存到文件中?您需要更加具体才能获得合适的答案。
  • this 的重复?

标签: asp.net json c#-4.0


【解决方案1】:

你真的应该看看Json.NET

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

【讨论】:

  • 我同意。为什么要发明轮子?
【解决方案2】:

有来自 System.Web.Extensions.dll (.NET 3.5 SP1) 的 JavaScriptSerializer

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

【讨论】:

    【解决方案3】:

    JavaScriptSerializer 怎么样?不错的集成解决方案,可以通过JavaScriptConverters 进行扩展。


    你所追求的演示:

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

    【讨论】:

    • 这似乎正是我所需要的。我不知道我是怎么错过的!非常感谢。
    猜你喜欢
    • 1970-01-01
    • 2012-11-06
    • 2011-07-25
    • 1970-01-01
    • 2021-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多