简体   繁体   English

如何在ASP.net中创建JSON数组?

[英]How to create JSON array in ASP.net?

i have created following format JSON array in PHP. 我已经在PHP中创建了以下格式的JSON数组 But now i want to create same format JSON array in ASP.net using C# code . 但是现在我想使用C#代码在ASP.net中创建相同格式的JSON数组。 How can i create it? 我如何创建它?

{
  "android": [
    {
      "name": "aaa",
      "version": "123"
    },
    {
      "name": "bb",
      "version": "34"
    },
    {
      "name": "cc",
      "version": "56"
    }
  ]
}

am using following method 我正在使用以下方法

  Public Class OutputJson
  {
  public List<Entity> Android { get; set; }
  }
  Public Class Entity
  {
  Public string Name { get; set; }
  Public string Version { get; set; }
  }
  outputJson = new OutputJson{
  Android = new List<Entity> 
 {
    new Entity { Name = "aaa", Version = "123"},
    new Entity { Name = "bb", Version = "34"},
    new Entity { Name = "cc", Version = "56"},
}   
var output = new  System.Web.Script.Serialization.JavaScriptSerializer().Serialize(outputJson); 

string YourJsonArray = output; 
Response.Clear(); 
Response.ContentType = "application/json; charset=utf-8"; 
Response.Write(YourJsonArray );
Response.End();

am receive following error 我收到以下错误

Runtime Error
Description: An application error occurred on the server. The current custom error settings for this application prevent the details of the application error from being viewed remotely (for security reasons). It could, however, be viewed by browsers running on the local server machine.

Details: To enable the details of this specific error message to be viewable on remote machines, please create a <customErrors> tag within a "web.config" configuration file located in the root directory of the current web application. This <customErrors> tag should then have its "mode" attribute set to "Off".

How can i solve it? 我该如何解决?

The easiest way is to add a json library to your project. 最简单的方法是将json库添加到您的项目。 For instance, with NuGet: 例如,使用NuGet:

Install-Package Newtonsoft.Json

Then, serialize your data: 然后,序列化您的数据:

struct VersionInfo
{
    public string name;
    public string value;
}

static void JsonExample()
{
    // create a dictionary as example
    var dict = new Dictionary<string, VersionInfo[]>();
    dict.Add("android", new VersionInfo[3]);
    dict["android"][0] = new VersionInfo { name = "aaa", value = "123" };
    dict["android"][1] = new VersionInfo { name = "bb", value = "34" };
    dict["android"][2] = new VersionInfo { name = "cc", value = "56" };

    var result = Newtonsoft.Json.JsonConvert.SerializeObject(dict);
}

This should produce the literal output in your example. 这应该在您的示例中产生文字输出。

you can use JavaScriptSerializer Class that comes with .NET framework. 您可以使用.NET框架随附的JavaScriptSerializer类

Namespace: System.Web.Script.Serialization 命名空间: System.Web.Script.Serialization

Assuming that your Outputjson is something like : 假设您的Outputjson是这样的:

Public Class OutputJson
{
    public List<Entity> Android { get; set; }
}

And Entity is like : 和实体就像:

Public Class Entity
{
    Public string Name { get; set; }
    Public string Version { get; set; }
}

So the using of JavaScriptSerializer Class will be like that : 因此,JavaScriptSerializer类的使用将像这样:

var outputJson = new OutputJson{
    Android = new List<Entity> 
    {
        new Entity { Name = "aaa", Version = "123"},
        new Entity { Name = "bb", Version = "34"},
        new Entity { Name = "cc", Version = "56"},
    }   
var output = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(outputJson); 

try this 尝试这个

 var s = new JavaScriptSerializer();
 string jsonClient = s.Serialize(customers);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM