0

Please kindly help me with this question. I've seen a question similar to mine, but it's closed and I didn't found the proper answer in it. See: Passing list of string values from aspx.cs to aspx

I got a json string which contains user's input in aspx.cs, and I want to pass that json string to aspx.

The code in aspx.cs is like below:

protected string GetUserData()
{
    // just some mock data
    Dictionary<string,string> userData = new Dictionary<string,string>();
    // question comes from double quotes and single quotes
    userData.Add("mock", "I\'m fine\"");
    return JsonConvert.SerializeObject(userData);
}

And the aspx code is like below:

<script type="text/javascript">
    window.userData = {
        cache: <%= GetUserData() %>
    };
</script>

This code works fine. But the format of window.userData.cache is Object instead of string. When I show it in console, it prints {mock: "I'm fine""}. It seems that asp.net have discarded the escape character and the outermost pair of double quotes for me. That's the key problem.

For some reason, I have to compare the origin variable assignment method with JSON.parse method in JavaScript. That means that I need the json string in origin format in aspx. I tried to modify my code in aspx to solve it with below codes.

cache: JSON.parse('<%= GetUserData() %>')

The problem is that window.userData.cache is JSON.parse('{"mock": "I\'m fine\""') now. The single quotes before { and m make a pair.

Here is the error message in Chrome:

Uncaught SyntaxError: Unexpected token ' in JSON at position xxxxx

I would appreciate it if you could kindly give me any idea. Thanks.

  • `For some reason, I have to compare the origin variable assignment method with JSON.parse method in JavaScript.` What does this mean? `But the format of window.userData.cache is Object instead of string.` Right... `It seems that asp.net have discarded the escape character and the outermost pair of double quotes for me.` No the Javascript engine did that, as per the specification. What are you actually trying to do here? Is there an X/Y issue going on with the question? http://xyproblem.info/ This answer might help clarify things for you? https://stackoverflow.com/a/2904181/426894 – asawyer Jul 15 '20 at 14:56
  • By the way, your code is not the same as the answer in the linked forums.asp.net post - they have string delimiters around the server render tags. Ie `cache: '<%= GetUserData() %>'` - That would cause the cache property to be a JSON string instead of a Javascript object. – asawyer Jul 15 '20 at 15:03
  • Oh! I think I see what you're getting at now. Try this: `userData.Add("mock", HttpUtility.JavaScriptStringEncode("I\'m fine\""));` - This will output `{"mock":"I\\u0027m fine\\\""}` Which is the Javascript object `{mock: "I\u0027m fine\""}` and `JSON.parse('{"mock":"I\\u0027m fine\\\""}')` *(The serialized string, not the Json object wrapped in single quotes)* will output the object `{mock: "I'm fine""}` - Notice the extra double quote is preserved. – asawyer Jul 15 '20 at 15:32

0 Answers0