locked
Converting JSON To plain JavaScript array. RRS feed

  • Question

  • User-1042970710 posted

    Dear Friends,

    I have list in C# which I converted to JSON string using Newton Soft and the Result is coming in this way.

    [{\"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}]

    But I actually want to supply my data to plain JavaScript array like that:

    var graphData= [
     ["Circulate Note", 5],
     ["Complaint", 46],
     ["Decision", 3],
     ["In", 3],
     ["Memo", 1]
    ];

    How I can convert the  Serialized JSON to Plain JavaScript array. 

    Monday, July 1, 2019 9:17 AM

Answers

  • User-474980206 posted

    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]]

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 1, 2019 5:10 PM

All replies

  • User753101303 posted

    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 ?

    Monday, July 1, 2019 10:01 AM
  • User-474980206 posted

    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]]

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 1, 2019 5:10 PM