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?
-
Duplicate for stackoverflow.com/questions/22918674/…Garry– Garry2015年07月01日 12:45:51 +00:00Commented Jul 1, 2015 at 12:45
-
am tried but not worksSasi Kumar– Sasi Kumar2015年07月01日 12:46:32 +00:00Commented Jul 1, 2015 at 12:46
-
1Then you should have added in your question, what you have tried and why it didnt worked and the error you are getting.Garry– Garry2015年07月01日 12:47:59 +00:00Commented Jul 1, 2015 at 12:47
-
Check out this topic: stackoverflow.com/questions/1056121/… (Newtonsoft.Json)Wolf5– Wolf52015年07月01日 12:54:41 +00:00Commented Jul 1, 2015 at 12:54
-
am android developer i dont know .net. please tell how to convert json array ?Sasi Kumar– Sasi Kumar2015年07月01日 13:00:03 +00:00Commented Jul 1, 2015 at 13:00
3 Answers 3
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.
7 Comments
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);
5 Comments
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 directlytry this
var s = new JavaScriptSerializer();
string jsonClient = s.Serialize(customers);