簡體   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