C# 将 System.Json 用于非 Silverlight 项目?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/775692/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 00:41:28  来源:igfitidea点击:

Using System.Json for non-Silverlight projects?

c#.netjson

提问by Patrick

Any idea on how to do it? If not possible, what's a good JSON library for C#?

关于如何做到这一点的任何想法?如果不可能,什么是 C# 好的 JSON 库?

回答by JonoW

If you're just looking for JSON encoding/decoding, there is an official System.Web extension library from Microsoft that does it, odds are you probably already have this assembly (System.Web.Extensions):

如果你只是在寻找 JSON 编码/解码,微软有一个官方的 System.Web 扩展库可以做到这一点,很可能你已经有了这个程序集(System.Web.Extensions):

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

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

Example:

例子:

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

class App 
{
    static void Main(string[] args = null)
    {
        JavaScriptSerializer jss = new JavaScriptSerializer();
        String sJson = "{\"Name\": \"Your name\"}";
        DesJson json = jss.Deserialize<DesJson>(sJson);

        Console.WriteLine(json.Name);
    }
}

class DesJson {
    public string Name {get; set;}
}

回答by Paul Suart

Here's an extenstion method to serialize any object instance to JSON:

这是将任何对象实例序列化为 JSON 的扩展方法:

public static class GenericExtensions
{
    public static string ToJsonString<T>(this T input)
    {
        string json;
        DataContractJsonSerializer ser = new DataContractJsonSerializer(input.GetType());
        using (MemoryStream ms = new MemoryStream())
        {
            ser.WriteObject(ms, input);
            json = Encoding.Default.GetString(ms.ToArray());
        }
        return json;
    }
}

You'll need to add a reference to System.ServiceModel.Web to use the DataContractSerializer.

您需要添加对 System.ServiceModel.Web 的引用才能使用 DataContractSerializer。

回答by Richard Fawcett

System.Jsonis now available in non-Silverlight projects via NuGet(.Net's package management system) and is hopefully going to be released as part of the core framework in vnext. The NuGet package is named JsonValue.

System.Json现在可以通过NuGet(.Net 的包管理系统)在非 Silverlight 项目中使用,并且有望作为 vnext 中核心框架的一部分发布。NuGet 包名为JsonValue

Imagine that we have the following JSON in the string variable json:

假设我们在字符串变量中有以下 JSON json

[{"a":"foo","b":"bar"},{"a":"another foo","b":"another bar"}]

We can get write the value "another bar" to the console using the following code:

我们可以使用以下代码将值“another bar”写入控制台:

using System.Json;
dynamic jsonObj = JsonValue.Parse(json);
var node = jsonObj[1].b;
System.Console.WriteLine(node.Value);

回答by Tal Aloni

Another option is to use Mono's implementation of System.Json, I was able to backport it to C# 2.0 with a few minor changes.

另一种选择是使用 Mono 的 System.Json 实现,我能够通过一些小的更改将其向后移植到 C# 2.0。

You can simply download my C# 2.0 project from here.

您可以简单地从这里下载我的 C# 2.0 项目。