简体   繁体   中英

Convert C# object to JSON or Javascript object

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?

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))

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);

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.

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)) );

OP的解决方案也适用于我。

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

If you have a C# Object and want to use it in JavaScript as is, you can do:

var jsObject = @Html.Raw(JsonConvert.SerializeObject(TheString));

You'll need to add the nuget package and import the dll in _ViewImports.cshtml :

@using Newtonsoft.Json;

Hope it helps someone...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM