简体   繁体   中英

How to create JSON array in ASP.net?

i have created following format JSON array in PHP. But now i want to create same format JSON array in ASP.net using C# code . 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. For instance, with 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.

Namespace: System.Web.Script.Serialization

Assuming that your Outputjson is something like :

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 :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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