繁体   English   中英

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

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

我已经在PHP中创建了以下格式的JSON数组 但是现在我想使用C#代码在ASP.net中创建相同格式的JSON数组。 我如何创建它?

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

我正在使用以下方法

  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();

我收到以下错误

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

我该如何解决?

最简单的方法是将json库添加到您的项目。 例如,使用NuGet:

Install-Package Newtonsoft.Json

然后,序列化您的数据:

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

这应该在您的示例中产生文字输出。

您可以使用.NET框架随附的JavaScriptSerializer类

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

假设您的Outputjson是这样的:

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

和实体就像:

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

因此,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); 

尝试这个

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

暂无
暂无

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

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