title: Sending JSON from C# to JavaScript
body: <p>If I am using JSON to serialize an object in C# what is the best/easiest way to send it from C#, receive it in Javascript and deserialize it in JS?</p>
  
  <p>Right now I am trying this but it doesn't seem to work</p>
  
  <p>C#      </p>
  
  <pre><code>json = JsonConvert.SerializeObject(X);
  ClientScript.RegisterArrayDeclaration("data", json);
  ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "id", "testFunc()", true);
  </code></pre>
  
  <p>JS</p>
  
  <pre><code>var data;
  function testFunc() {
     d3.select("#stuff").append("h3").text(data.source);
  }
  </code></pre>
  
  <p><strong>EDIT</strong>
  To clarify 'doesn't work'. I walk through the C# code sending the json and the string sent looks like this </p>
  
  <pre><code>"{\"target\":0,\"source\":1}"
  </code></pre>
  
  <p>I send it to the above javascript and it prints nothing to my page.</p>
  
  <p>I looked at the browsers debugger and saw this line</p>
  
  <pre><code>var data =  new Array({"target":0,"source":1});
  </code></pre>
  
  <p>So, from my limited knowledge for some reason the data is being sent to the var in JS, not as a string but as a new array.</p>
  
  <p>So I changed my JS to </p>
  
  <pre><code>var data;
  function testFunc() {
      d3.select("#stuff").append("h3").text("Source: " + data[0].source + " Target " + data[0].target);
  }
  </code></pre>
  
  <p>And voila the values printed out but why doesn't the var receive the JSON string as the C# sent? How can I get it to just send the string so I can parse it using JSON.parse()? I want to eventually send a whole list of objects and it will be easier to have the data arrive as the intended JSON string.</p>