And you get this string how to the JavaScript side. It seems you are looking for https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse.
If using Ajax it should be done for you (a common problem is having a service that returns explicitely a json string which is serialized again automatically by ASP.NET (rather than returning directly the object and let ASP.NET to serialize it).
Or do you meant you want to generate JavaScript code ?
as your inner array has string and number elements (bad design), you need to make the C# the same (use object)
//c#
var list = new List<object[]> {
new object[] {"Circulate Note", 5},
new object[] {"Complaint", 46},
new object[] {"Decision", 3},
new object[] {"In", 3},
new object[] {"Memo", 1}
};
or in javascript you could convert the json to your desired format:
var json = "[{\"CorrespondanceType\":\"Circulate Note\",\"NoOfCorrespondence\":5},{\"CorrespondanceType\":\"Complaint\",\"NoOfCorrespondence\":46},{\"CorrespondanceType\":\"Decision\",\"NoOfCorrespondence\":3},{\"CorrespondanceType\":\"In\",\"NoOfCorrespondence\":1168},{\"CorrespondanceType\":\"Memo\",\"NoOfCorrespondence\":78},{\"CorrespondanceType\":\"Out\",\"NoOfCorrespondence\":1703},{\"CorrespondanceType\":\"Secret In\",\"NoOfCorrespondence\":2},{\"CorrespondanceType\":\"Secret Out\",\"NoOfCorrespondence\":1}]";
var o = JSON.parse(json).map(function(v) {
return [Object.values(v)[0], Object.values(v)[1]];
});
console.log(o); //[["Circulate Note",5],["Complaint",46],["Decision",3],["In",1168],["Memo",78],["Out",1703],["Secret In",2],["Secret Out",1]]
Member
39 Points
96 Posts
Converting JSON To plain JavaScript array.
Jul 01, 2019 09:17 AM|jalali|LINK
Dear Friends,
I have list in C# which I converted to JSON string using Newton Soft and the Result is coming in this way.
But I actually want to supply my data to plain JavaScript array like that:
How I can convert the Serialized JSON to Plain JavaScript array.
All-Star
48490 Points
18070 Posts
Re: Converting JSON To plain JavaScript array.
Jul 01, 2019 10:01 AM|PatriceSc|LINK
Hi,
And you get this string how to the JavaScript side. It seems you are looking for https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse. If using Ajax it should be done for you (a common problem is having a service that returns explicitely a json string which is serialized again automatically by ASP.NET (rather than returning directly the object and let ASP.NET to serialize it).
Or do you meant you want to generate JavaScript code ?
All-Star
58124 Points
15635 Posts
Re: Converting JSON To plain JavaScript array.
Jul 01, 2019 05:10 PM|bruce (sqlwork.com)|LINK
as your inner array has string and number elements (bad design), you need to make the C# the same (use object)
or in javascript you could convert the json to your desired format: