JSON在C#(preferably没有第三方工具) [英] JSON in C# (preferably without third-party tools)

查看:99
本文介绍了JSON在C#(preferably没有第三方工具)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过的在C#中相当多的方式来输出JSON。他们都没有,但是,似乎很容易。这很可能是由于我缺乏C#的知识,虽然。

问题:如果你写JSON(用于JavaScript的后面),你会怎么做呢?我想preFER不使用第三方工具;然而,这不是任何形式的最后通牒。 JSON是C#中的ASP.NET可行的开箱即用的?

JSON的例子我希望写:

  {
    旅行:[
        {
            从这里,
            要:有,
            停止:
                {
                    地方:middle1
                    KM的:37
                },
                {
                    地方:middle2
                    KM的:54
                }
            ]
        },
        {
            从那里,
            到这里,
            停止:
                {
                    地方:middle2
                    KM的:37
                },
                {
                    地方:middle1
                    KM的:54
                }
            ]
        }
    ]
}


解决方案

如何<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx\"相对=nofollow>的JavaScriptSerializer ?尼斯集成的解决方案,让其在与<一个扩展href=\"http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptconverter.aspx\"相对=nofollow> JavaScriptConverter 秒。


你是什么演示后:

 使用系统;
使用System.Web.Script.Serialization;
使用System.Text;公共类停止
{
    公共字符串的地方{搞定;组; }
    公众的Int32 KM的{搞定;组; }
}公共类旅行
{
    从{得到公众的字符串;组; }
    公共字符串为{搞定;组; }
    市民停止[] {站搞定;组; }
}公共类MyJSONObject
{
    公共跳闸[] {行程搞定;组; }    公共重写字符串的ToString()
    {
        StringBuilder的SB =新的StringBuilder();
        的foreach(在this.Trips旅行行程)
        {
            sb.AppendFormat(旅行:\\ r \\ n);
            sb.AppendFormat(\\ t若要:{0} \\ r \\ n,trip.to);
            sb.AppendFormat(\\ t来源:{0} \\ r \\ n,trip.from);
            sb.AppendFormat(\\ tStops:\\ r \\ n);
            的foreach(在trip.stops停止停止)
            {
                sb.AppendFormat(\\ t \\ tPlace:{0} \\ r \\ n,stop.place);
                sb.AppendFormat(\\ t \\ t公里:{0} \\ r \\ n,stop.KMs);
            }
            sb.AppendLine();
        }
        返回sb.ToString();
    }
}
公共类测试
{
    公共静态无效的主要()
    {
        例如字符串= \"{\\\"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 =新的JavaScriptSerializer();        //解析字符串,我们的自定义对象
        MyJSONObject结果= serializer.Deserialize&LT; MyJSONObject&GT;(例子);
        Console.WriteLine(对象反序列化:\\ r \\ n \\ r \\ n {0} \\ r \\ n \\ r \\ n,结果);        //把我们的自定义对象,并在一个字符串转储
        StringBuilder的SB =新的StringBuilder();
        serializer.Serialize(结果是,某人);
        Console.WriteLine(对象重新序列:\\ r \\ n \\ r \\ n {0} \\ r \\ n \\ r \\ n,某人);        //检查步骤
        Console.WriteLine(example.Equals(sb.ToString())MATCH:不匹配);
    }
}

和输出:

 对象序列化:行程:
           要:有
         从这里
        停止:
                地点:middle1
                  公里:37
                地点:middle2
                  公里:54行程:
           到这里
         从那里
        停止:
                地点:middle2
                  公里:37
                地点:middle1
                  公里:54
对象重新序列:{旅行:[{从:在这里,要:有,停止:[{地方:middle1,KM的:37},{PL
王牌:middle2,KM的:54}]},{从:有,要:在这里,停止:[{地方:中间
2,KM的:37},{地方:middle1,KM的:54}]}]}匹配
preSS任意键继续。 。 。

I've seen quite a few ways to output JSON in C#. None of them, however, seem easy. This may very well be due to my lack of knowledge of C#, though.

Question: If you were to write JSON (for use in JavaScript later on), how would you do it? I would prefer to not use third-party tools; however this is not an ultimatum of any kind. Is JSON doable in C# ASP.NET out of the box?

Example of JSON I wish to write:

{
    "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
                }
            ]
        }
    ]
}

解决方案

How about JavaScriptSerializer? Nice integrated solution, allowed to be expanded upon with JavaScriptConverters.


Demo of what you were after:

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");
    }
}

And the output:

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

这篇关于JSON在C#(preferably没有第三方工具)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆