0

A list that contains a set of arrays is being serialized into jquery. However when check in client side, it is receiving only a string instead of the array object.

c#:

public string jsscript(){
// datatable processing
    var arrList = new List<object>();

    foreach (DataRow row in table.Rows)
    {
        string name = row[0].ToString();
        string quantity = row[1].ToString();
        string balance = row[2].ToString();
        string remove = "X";

        arrList.Add( new[] { name, quantity, balance, remove });
    }

    return (new JavaScriptSerializer()).Serialize(arrList);
 }

js:

<script>
    //dom... 
    function theDomHasLoaded(e) {
            dbdata = <%=jsscripts()%>;  

</script>
Tim
  • 14,999
  • 1
  • 45
  • 68
aspiring
  • 1,557
  • 2
  • 20
  • 43
  • 1
    but the point of serialise is to generate a json string... Are you expecting an object? You'll need to use JSON.parse(...) – Dave Lawrence May 29 '15 at 14:21
  • What does that ` – Pointy May 29 '15 at 14:41
  • @daveL Then I am missing `JSON.parse`. – aspiring May 29 '15 at 14:44
  • @Pointy View Source : `dbdata = [["Jorge","25","20","X"],["Jacques","50","10","X"],["Bell","10","10","X"]]; ` it's in proper format But when `alert(dbdata)` : it's just a string delimited by comma. – aspiring May 29 '15 at 14:46
  • I have no idea why someone voted this to be an off topic: `This question does not appear to be about programming within the scope defined in the help center.`... – aspiring May 29 '15 at 14:47
  • @aspiring that's because when you pass the array to `alert()` it's forced to be a string; that's just what `alert()` does. Use `console.log()` instead - it's much more useful. – Pointy May 29 '15 at 16:29

1 Answers1

0

see JSON string to JS object

you need to use JSON.parse(..) on the serialised string

that is of course presuming that that is the problem... It's not entirely clear what your problem is.

Community
  • 1
  • 1
Dave Lawrence
  • 3,843
  • 2
  • 21
  • 35
  • This is not necessarily the right thing to do. If that serializer really creates a JSON string, then dropping the string straight into JavaScript will result in a valid JavaScript object initializer expression, because JSON is valid JavaScript. There should be no need to embed a string containing JSON and then explicitly parse it. – Pointy May 29 '15 at 14:40
  • @Pointy dbdata will be initialised as a string... as the server will just write a string to the aspx/view page and then send that to the client. The client does not see that as json. Just a string. – Dave Lawrence May 29 '15 at 14:44
  • You (the client) are not receiving anything. The server writes the string to the aspx/view and then serves that to the client. The client does not see a json object. It sees a string. If you want the client to have an object; you'll need to create an object from that string. – Dave Lawrence May 29 '15 at 14:51
  • If client is not seeing a JSON object, why is this happening? It fails to convert here. After using `JSON.parse` I get this in View Source : `dbdata = JSON.parse([["Jorge","25","20","X"],["Jacques","50","10","X"],["Bell","10","10","X"]]);` – aspiring May 29 '15 at 14:51
  • Why would it be seeing a JSON object? You are writing a string to the page on the server not returning a json string with a mime type of application/json – Dave Lawrence May 29 '15 at 15:01
  • It's returning a JSON string without `JSON.parse`, otherwise what's the meaning of `JavaScriptSerializer()).Serialize` this code and view source in this format `dbdata = [["Jorge","25","20","X"],["Jacques","50","10","X"],["Bell","10","10","X"]];`? It looks like something else, which you and I both are unaware seems to happen... – aspiring May 29 '15 at 15:29
  • 1
    @daveL see the comment the OP added to the question. As I suspected, the JSON is being dumped directly into the ` – Pointy May 29 '15 at 16:30
  • @Pointy I've personal experience of doing something exactly like this... And my experience says that it is nothing more than a string. I'd do a demo if I had the time but I don't.. – Dave Lawrence Jun 02 '15 at 12:13