将 C# 对象转换为 JSON 或 Javascript 对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19546426/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert C# object to JSON or Javascript object
提问by Daebarkee
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)
我是一名 C# 开发人员,也是一名 Javascript 新手。我有一个 C# 对象,最后,在 index.cshtml 中,我可以通过调用 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:
但是,当我调用 JSON.parse(@TheString) 时,我得到:
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.
我在这里看不到任何“o”。
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?
另外,为了制作 javascript 对象,Travis 建议在序列化之前删除键名。但在 C# 中,所有对象都必须有其成员名称。我能想到的就是字符串操作。有没有更好的方法来做到这一点?
回答by p.s.w.g
回答by Andy W
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:
对于您的第二个错误,JSON.parse 需要一个字符串,但您传入的是一个数组。您输出的 js 代码必须如下所示才能工作:
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:
我还想指出,由于您将此对象注入服务器端的 javascript 代码中,因此根本不需要调用 JSON.parse。只要您将格式正确的 javascript 发送到将被评估和运行的客户端,那么它在服务器上的创建方式并不重要。试试这个:
var data1 = @Html.Raw(@tmp);
回答by Rahul Tripathi
You may try this using HtmlHelper.Raw method:-
您可以尝试使用HtmlHelper.Raw 方法:-
data = JSON.parse(@Html.Raw(TheString));
Also check out DataContractJsonSerializer Class
另请查看DataContractJsonSerializer 类
Serializes objects to the JavaScript Object Notation (JSON) and deserializes JSON data to objects. This class cannot be inherited.
将对象序列化为 JavaScript 对象表示法 (JSON) 并将 JSON 数据反序列化为对象。这个类不能被继承。
回答by Travis J
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
使用该字符串将使 Razor 保护您免受注射。如果您传入 json,那可能不是问题。常见的做法是使用Html.Raw
helper
data = JSON.parse( @(Html.Raw(TheString)) );
回答by Vijayanand Settin
OP's solution worked for me as well.
OP 的解决方案也适用于我。
data = eval(JSON.parse(@Html.Raw(TheString)))
回答by Mayer Spitzer
If you have a C# Object
and want to use it in JavaScript as is, you can do:
如果你有一个 C#Object
并且想在 JavaScript 中按原样使用它,你可以这样做:
var jsObject = @Html.Raw(JsonConvert.SerializeObject(TheString));
You'll need to add the nuget package
and import the dll
in _ViewImports.cshtml
:
您需要添加nuget package
并导入dll
in _ViewImports.cshtml
:
@using Newtonsoft.Json;
Hope it helps someone...
希望它可以帮助某人...