0

In my javascript code I am getting json string from cs file

var tmpString="<%=resultset2%>";    

In cs I am concatenating strings to build json string. Here is an issue the json string is returned as a string and it has " with it.

"[{id:'1',name:'Aik'},{id:'2',name:'Aik or Aik'}]"

Because of " in beginning and end javascript code treat it as a string. Kindly guide me how I should sort out this issue.

thanks

user576510
  • 5,777
  • 20
  • 81
  • 144

2 Answers2

5
  1. Fix the JSON, it has errors (property names must be strings (and thus quoted), and only " are acceptable for quoting strings in JSON). JSON is a subset of JavaScript, you can't use all of JS' syntax in JSON. As a rule of thumb, if you are concatenating strings to produce a data format, then you are doing it wrong. http://json.org/ lists a number of C# libraries that you can use to build JSON.
  2. Use json2.js
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Thanks @David, is it possible doing on client side ? if I try doing it on server side it will requrie a lot of work to do in changes. – user576510 Apr 05 '11 at 16:33
  • If you really, really want to just hack together support for treating that string as an object instead of working with JSON … then just dump the JS object instead of making it a string: `var tmpString=<%=resultset2%>;` – Quentin Apr 05 '11 at 16:34
  • 1
    I was about to add another answer to the effect of "why use JSON at all if you're just rendering javascript from a server page?" and I realized, he's not using JSON. He's just trying to render Javascript object syntax. There's no data transfer here, it's just server code writing javascript, so no point in using JSON and adding another layer. – Jamie Treworgy Apr 05 '11 at 16:35
  • There is a point in using JSON — it can be cleanly generated from mature libraries and thus reduce the risk of XSS problems generated by user data. – Quentin Apr 05 '11 at 16:40
  • @jamietre. It is JSON. It's the easiest way for your server side to pass values to your client side JS. Doing it from the page itself (instead of an XHR) saves a trip to the server. – Ruan Mendes Apr 05 '11 at 16:59
  • @Juan Mendes - I understand that, but you can write client script that is rendered with JSON and then deserializes it, or you can just render javascript object syntax directly. By all appearances he was trying to do the latter, so it's not JSON. @David D. I see your point. – Jamie Treworgy Apr 05 '11 at 17:19
  • @jamietre: That's true, the question is about deserializing. But the important thing for the OP, IMO, is that you don't need to deserialize it in this case. And you're still using JSON :) – Ruan Mendes Apr 05 '11 at 21:20
  • @Juan, no. The OP isn't deserializing anything. He was simply trying to render JS code that creates an object, and failing because he had double quotes around it. There's no JSON here. He just called it JSON, because he (and half of the world) thinks that JSON is the same thing as JS code that defines objects, but it's not. – Jamie Treworgy Apr 05 '11 at 21:40
  • @jamietre: You're splitting hairs here, the OP was clearly trying to deserialize malformed JSON. Telling him/her that he's not using JSON is of no help. Letting them know that their JSON is malformed and that it doesn't need to be quoted since it can be parsed directly by JavaScript is the info the OP needs. – Ruan Mendes Apr 06 '11 at 17:28
  • That's pretty much exactly what I told him in my answer which he accepted :) I'm just debating with you that this is or isn't, technically, json. Something which "can be parsed directly by JavaScript" is not JSON. JSON must be deserialized. JSON has quotes around the name value. – Jamie Treworgy Apr 06 '11 at 17:33
2

Change this:

var tmpString="<%=resultset2%>";

to:

var tmpString=<%=resultset2%>;

This isn't JSON, you're just writing javascript from a server page. The problem is you are creating invalid javascript syntax, you just need to remove the quotes.

The quotes aren't from resultset2 they are from your markup.

Jamie Treworgy
  • 23,934
  • 8
  • 76
  • 119