C# 在 .NET 2.0 中序列化为 JSON
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1056169/
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
Serialize to JSON in .NET 2.0
提问by PositiveGuy
how can you serialize an object to JSON in .NET 2.0 using C#?
如何在 .NET 2.0 中使用 C# 将对象序列化为 JSON?
回答by lavinio
JSON.orghas references to a number of serializers in a number of languages, including more than half a dozen in C#. You should be able to find one which meets your API and licensing needs including JSONsharpwith the LGPL license and the well-designed Json.NET.
JSON.org引用了多种语言的许多序列化程序,其中包括 C# 中的六个。您应该能够找到一种满足您的 API 和许可需求的产品,包括具有 LGPL 许可的JSONsharp和精心设计的Json.NET。
If what you're serializing is fairly simple, it's not all that hard to write your own for a specific purpose. The JSON.orgsite has the syntax, and it's very straight-forward.
如果您要序列化的内容相当简单,那么为特定目的编写自己的序列并不难。该JSON.org网站有语法,这是非常直接的。
回答by Joe Chung
You can use the JavaScriptSerializerclass from ASP.NET Ajax 1.0, which is compatible with .NET 2.0.
您可以使用ASP.NET Ajax 1.0 中的JavaScriptSerializer类,它与 .NET 2.0 兼容。
回答by Joe Chung
Are you trying to build an RPC server in the .NET side? If so look at Jayrock (jayrock.berlios.de). You get the source code and it will compile under 2.0.
您是否正在尝试在 .NET 端构建 RPC 服务器?如果是这样,请查看 Jayrock (jayrock.berlios.de)。你得到源代码,它将在 2.0 下编译。
Also setting an RPC server is a snap:
设置 RPC 服务器也很简单:
using Jayrock;
using Jayrock.JsonRpc;
using Jayrock.JsonRpc.Web;
using Jayrock.Json;
using Jayrock.Json.Conversion;
namespace myRPCService
{
[JsonRpcService("Service")]
public class Service : JsonRpcHandler
{
[JsonRpcMethod("call", Idempotent = true)]
public string call(string value)
{
JsonObject oJSON = JsonConvert.Import(typeof(JsonObject), value) as JsonObject;
...
return oJSON.ToString();
}
}
}
回答by Praveena M
I use below code for JSON message and works well for me.
我将以下代码用于 JSON 消息,对我来说效果很好。
public static class JSONHelper
{
public static string ToJSON(this object obj)
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
return serializer.Serialize(obj);
}
}
Calling JSON serializer in WCF.
在 WCF 中调用 JSON 序列化程序。
[OperationContract]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json)]
public string LoadDetails()
{
LogDetails objSubLog = new LogDetails ();
List<LogDetails> lstLogdetails;
DAL objDAL = new DAL();
lstLogdetails = objDAL.GetLog("ALL", objSubLog);
return lstLogdetails.ToJSON();
}
回答by Tal Aloni
I was able to backport Mono's implementation of System.Json to C# 2.0 with a few minor changes.
我能够通过一些小的更改将 Mono 的 System.Json 实现向后移植到 C# 2.0。
You'll need 6 files from hereor you can simply download my C# 2.0 project from here.
你会从需要6个文件在这里,或者你可以简单地从下载我的C#2.0的项目在这里。
Note that with System.Json you'll have to manually serialize any non-primitive data type. (see here)
请注意,对于 System.Json,您必须手动序列化任何非原始数据类型。(见这里)