在 C# 中创建 Json

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11560789/
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-09 18:19:24  来源:igfitidea点击:

Create Json in C#

c#json

提问by AliR?za Ad?yah?i

Possible Duplicate:
How to create JSON string in C#

可能的重复:
如何在 C# 中创建 JSON 字符串

I want to use google chart in mt ASP.NET MVC application. There is no mvc examples in there and datatable struct. I have never worked Json before and I must create a json like following code.

我想在 mt ASP.NET MVC 应用程序中使用谷歌图表。那里没有 mvc 示例和数据表结构。我以前从未使用过 Json,我必须像下面的代码一样创建一个 json。

How can I create like this in C#

我如何在 C# 中创建这样的

{"cols":[
  {"id":"","label":"Month","pattern":"","type":"string"},
  {"id":"","label":"Sales","pattern":"","type":"number"},
  {"id":"","label":"Expenses","pattern":"","type":"number"}],
"rows":[
  {"c":[
    {"v":"April","f":null},
    {"v":1000,"f":null},
    {"v":900,"f":null},
  {"c":[
    {"v":"July","f":null},
    {"v":1030,"f":null},
    {"v":null,"f":null},
"p":null
}

I cant find easy examples about creating json in C#. Please Help me.

我找不到有关在 C# 中创建 json 的简单示例。请帮我。

Thanks.

谢谢。

采纳答案by Usman

回答by paulslater19

Create it as a normal class with properties, then:

将其创建为具有属性的普通类,然后:

var json = new JavaScriptSerializer().Serialize(myObject)

or a dynamic object:

或动态对象:

var json = new JavaScriptSerializer().Serialize(new { property = "string" })

回答by Jigar Pandya

Well you can use DataContractJsonSerializer Classto Serializes objects to the JavaScript Object Notation (JSON) and deserializes JSON data to objects. in .net 4.0.

那么您可以使用DataContractJsonSerializer 类将对象序列化为 JavaScript 对象表示法 (JSON) 并将 JSON 数据反序列化为对象。在 .net 4.0 中。

I hope you have checked How to create JSON string in C#and this is not duplication of the same.

我希望您已经检查了如何在 C# 中创建 JSON 字符串,这不是相同的重复。

Apart from that you can have option to use WCF services that produce RESTful Service and JSON data. You can check this example that is best exampleto suit your needs in WCF.

除此之外,您可以选择使用生成 RESTful 服务和 JSON 数据的 WCF 服务。您可以查看此示例,该示例是最适合您在 WCF 中需求的示例

Another approach is if you like some built in library then Json.Netis one of the good library around on codeplex.

另一种方法是,如果您喜欢一些内置库,那么Json.Net是 codeplex 上的好库之一。

回答by David

I would try the JSON.NET library. It has advantages of most of the serializers built into .NET in terms of both capability and performance. And I believe Microsoft will be bundling the JSON.NET library with ASP.NET 4.5 as a result.

我会尝试JSON.NET 库。它在功能和性能方面都具有 .NET 内置的大多数序列化程序的优势。我相信 Microsoft 将因此将 JSON.NET 库与 ASP.NET 4.5 捆绑在一起。

回答by Qasim Javaid Khan

Have a look at this code.

看看这个代码。

    var oSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
    oSerializer.MaxJsonLength = int.MaxValue;
    string sa = oSerializer.Serialize(p); // where p is your object to serialize.

    sa = "{ \"Result\": " + sa + " }";

回答by Miklar

Give Json.NETa try. I think it will give you what you need.

Json.NET一试。我认为它会给你你所需要的。