Convert C# object to JSON or Javascript object

22,344

Solution 1

Razor will automatically escape HTML entities for you in an attempt to be helpful. You can disable this with Html.Raw:

JSON.parse(@Html.Raw(TheString))

Solution 2

For your second error, JSON.parse expects a string, but you are passing in an array. Your outputted js code has to look like this to work:

var data1 = JSON.parse("[{\"Name\":\"CASE_A\",\"Values\":[99.8,99.9,98.6]},{\"Name\":\"CASE_B\",\"Values\":[96.7,11.1]}]");

I also want to note that since you are injecting this object into your javascript code on the server side, there is no need to call JSON.parse at all. As long as you send properly formatted javascript to the client where it will be evaluated and run, then it doesn't matter how it's created on the server. Try this instead:

var data1 = @Html.Raw(@tmp);

Solution 3

You may try this using HtmlHelper.Raw method:-

data = JSON.parse(@Html.Raw(TheString));

Also check out DataContractJsonSerializer Class

Serializes objects to the JavaScript Object Notation (JSON) and deserializes JSON data to objects. This class cannot be inherited.

Solution 4

Using the string will cause Razor to protect you from injection. If you are passing in json chances are that isn't an issue. Common practice is to use the Html.Raw helper

data = JSON.parse( @(Html.Raw(TheString)) );
Share:
22,344
Daebarkee
Author by

Daebarkee

Updated on July 16, 2022

Comments

  • Daebarkee
    Daebarkee almost 2 years

    I am a C# developer and a newbi in Javascript. I have one C# object and finally, in index.cshtml, I can get a string converted from the object via calling Json.Encode(obj)

    The string is:

    [
        {
        "Name":"CASE_A",
        "Values":[99.8,99.9,99.9,99.8,99.8,96.3,22.3]
        },
        {
        "Name":"CASE_B",
        "Values":[99.8,99.8,99.8,96.3,22.3]
        },
    ]
    

    However, when I call JSON.parse(@TheString),I got:

    Uncaught SyntaxError: Unexpected token & 
    

    The location of this error shows me this:

    data = JSON.parse([{"Name":"CASE_A","Values":[99.8,99.9,99.9,99.8 ....
    

    How can I fix this problem?


    Thank you for the answers! But still I got an error:

    Uncaught SyntaxError: Unexpected token o
    

    For simple testing, I used this:

    @{
        string tmp = "[{\"Name\":\"CASE_A\",\"Values\":[99.8,99.9,98.6]},{\"Name\":\"CASE_B\",\"Values\":[96.7,11.1]}]";
    }
    var data1 = JSON.parse(@Html.Raw(@tmp));
    

    And source shows this line:

    var data1 = JSON.parse([{"Name":"CASE_A","Values":[99.8,99.9,98.6]},{"Name":"CASE_B","Values":[96.7,11.1]}]);
    

    I cannot see any "o" here.


    Also, for making javascript object, Travis suggested to remove the key name before serialization. But in C#, all object must have its member name. All I can think of is string manipulation. Is there any better way to do so?

    • Daebarkee
      Daebarkee over 10 years
      Ideally, it would be much better if I can get javascipt object without Key Name. e.g., [[CASE_A, [[99.8,99.9,99.9,99.8,99.8,96.3,22.3], ...]
    • Travis J
      Travis J over 10 years
      That should be done before you serialize.
    • Daebarkee
      Daebarkee over 10 years
      I actually don't figure this error out because of anther error: "Uncaught SyntaxError: Unexpected token o". But instead of json object, I used a javascript object and used "data= eval(@Html.Raw(TheString))". It was successful.
  • Daebarkee
    Daebarkee over 10 years
    Thank you Rahul. But still I have an issue. Please, see the updated question.
  • Daebarkee
    Daebarkee over 10 years
    Thank you p.s.w.g. I was wondering between Rahul and you but it seems more people liked your clear answer.
  • p.s.w.g
    p.s.w.g over 10 years
    @Daebarkee Thanks, but keep in mind, you should generally accept the answer which you feel most completely answers your question or resolves your issue, which isn't necessarily the most popular one. See also What does it mean when an answer is "accepted"?