0

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?

sasikumar
  • 12,540
  • 3
  • 28
  • 48

3 Answers3

6

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.

  • is it possible to create json array without add library? because the FTP controls not in myside. – sasikumar Jul 01 '15 at 13:05
  • I don't really understand what you're asking here. When you compile your program the Newtonsoft.Json library is just a dll you ship with your program. What does FTP have to do with that? – Martin Watts Jul 01 '15 at 13:17
  • why use newtonsoft at all? – naveen Jul 01 '15 at 13:18
  • That's an existing [SO question](http://stackoverflow.com/questions/11263166/json-net-jsonconvert-vs-net-javascriptserializer) – Martin Watts Jul 01 '15 at 13:55
  • _And_ Newtonsoft.Json is about one third of the size of System.Web.Extensions _and_ System.Web.Extensions isn't available in the Client Profile. – Martin Watts Jul 01 '15 at 14:10
  • Currently your code is not even close to compiling, let alone running correctly. – Martin Watts Jul 02 '15 at 05:31
  • To get you started, start a new Asp.Net project in Visual Studio, add a WebForm, and paste your code in the code behind file. The class definitions for OutputJson and Entity should go outside the WebForm class definition, and the code starting at outputJson = ... inside the page load. Also, remember all keywords in C# are lower case, so it's "public class", not "Public Class". You are also missing some closing curly braces before "var output = ..." – Martin Watts Jul 02 '15 at 05:37
3

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); 
  • all code in same file or separate file? am android developer i dont know .net. – sasikumar Jul 01 '15 at 13:07
  • 1
    You can insert each part of code in a separate Class file ( the efficient way ) ( OutputJsonOutputJson.cs for OutputJson Class and Entity.cs for Entity Class ) and then instantiate them where do you need them (the place where you want serialize them as Json.). – Zakaria Smahi Jul 01 '15 at 13:12
  • In your code how can i print jsonarray in my page(test.asp)? – sasikumar Jul 01 '15 at 13:15
  • 1
    You can use Response.write() method to print your json : `string YourJsonArray = JsonArrayValue; Response.Clear(); Response.ContentType = "application/json; charset=utf-8"; Response.Write(YourJsonArray ); Response.End();` Use this code in Page_Load Event if you want to display it directly – Zakaria Smahi Jul 01 '15 at 13:21
  • This issue occurs when a custom error page in the application also contains an error. according to [support.microsoft.com](https://support.microsoft.com/en-us/kb/910434), you can resolve it by adding a static page as an Error page inside your project and add it in your web.config file as suggested Visual Studio in Compiling Errors Log – Zakaria Smahi Jul 02 '15 at 11:17
1

try this

 var s = new JavaScriptSerializer();
 string jsonClient = s.Serialize(customers);
manish
  • 68
  • 1
  • 5
  • am android developer i dont know .net. please tell how to convert json array with details – sasikumar Jul 01 '15 at 13:00
  • yes it possible you can use System.Web.Script.Serialization; namespace with out adding any library, its a .net core library – manish Jul 01 '15 at 13:12