javascript 有没有办法将 JSON 对象列表从 JS 传递到 C#?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4247152/
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
Is there a way to pass a list of JSON objects from JS to C#?
提问by user469652
Something I'm confusing. The Javascript is going to produce the following JSON data.
我很困惑的东西。Javascript 将生成以下 JSON 数据。
{type:"book" , author: "Lian", Publisher: "ABC"}
{type:"Newspaper", author: "Noke"}
This is only an example, actually I've got more than this.
这只是一个例子,实际上我有更多。
Since I have common fields between different JSON data, so I don't know is it possible to pass this to C# at one time. What I want to do is pass this to c# then do some processing, what is the best way to do? I'm using ASP.NET MVC2.
由于我在不同的 JSON 数据之间有公共字段,所以我不知道是否可以一次将其传递给 C#。我想要做的是将它传递给 c# 然后做一些处理,最好的方法是什么?我正在使用 ASP.NET MVC2。
Thanks for your answer or hints.
感谢您的回答或提示。
回答by Matt
The combination of the 2 JSON statements above are, together, not valid JSON. That being said, you will not be able to use the JavaScriptSerializer class to deserialize that data into c# structure directly. Instead you will have to do some manual parsing first, to either break it down into valid JSON or just do full on manual parsing.
上面 2 个 JSON 语句的组合在一起不是有效的 JSON。话虽如此,您将无法使用 JavaScriptSerializer 类直接将该数据反序列化为 c# 结构。相反,您必须先进行一些手动解析,将其分解为有效的 JSON 或仅完成手动解析。
What I would actually recommend is sending over valid JSON instead. You can accomplish this by doing something like this:
我实际上推荐的是通过有效的 JSON 发送。您可以通过执行以下操作来完成此操作:
{list: [
{type:"book" , author: "Lian", Publisher: "ABC"},
{type:"Newspaper", author: "Noke"} ]
Hard to say exactly, since only you know the details of your use case. You can send this data over using a traditional 'ajax' request. This is very easy to do with out any of the many JS libraries out there, but I would recommend just going with one anyway - they offer higher level constructs that are easier to use (and address cross-browser idiosyncrasies).
很难说清楚,因为只有你知道你的用例的细节。您可以使用传统的“ajax”请求发送此数据。如果没有许多 JS 库中的任何一个,这很容易做到,但我建议无论如何只使用一个 - 它们提供更易于使用的更高级别的构造(并解决跨浏览器的特性)。
Since you are using ASP.NET MVC2, I would recommend jQuery. Microsoft is now backing jQuery as their JS library of choice and even make it default for new web projects.
由于您使用的是 ASP.NET MVC2,我会推荐 jQuery。微软现在支持 jQuery 作为他们选择的 JS 库,甚至将其作为新 Web 项目的默认库。
Once you pass the above JSON to C#, you can deserialize it by doing something like this:
将上述 JSON 传递给 C# 后,您可以通过执行以下操作对其进行反序列化:
JavaScriptSerializer serializer = new JavaScriptSerializer();
var result = serialzer.Deserialize<Dictionary<string, object>>(postedJSONData);
Your result will then have a structure that looks like this, in C#:
您的结果将具有如下所示的结构,在 C# 中:
Dictionary<string, object> result =>
{ "list" => object },
object => List<object>,
List<object> => Dictionary<string, object>
{ "type" => "book", "author" => "Lian" } // etc
回答by BGerrissen
[
{type:"book" , author: "Lian", Publisher: "ABC"},
{type:"Newspaper", author: "Noke"}
]
Is still valid JSON (well actually keys need to be enclosed in " as well), so you can .push()into an array each time you create a JSON record.
仍然是有效的 JSON(实际上键也需要包含在 " 中),因此.push()每次创建 JSON 记录时都可以放入一个数组中。
var list = [];
// code doing other stuff
list.push({type:"book" , author: "Lian", Publisher: "ABC"});
// more code doing other stuff
list.push({type:"Newspaper", author: "Noke"})
Once your JSON list is constructed, you can send that list to the backend in one go.
构建 JSON 列表后,您可以一次性将该列表发送到后端。
回答by Seattle Leonard
You can also use the JavaScriptSerializer to deserialize your own custom type. Esentially you make a very simple type with all the properties of your json objects then call
您还可以使用 JavaScriptSerializer 反序列化您自己的自定义类型。本质上,您使用 json 对象的所有属性创建了一个非常简单的类型,然后调用
JavaScriptSerializer serializer = new JavaScriptSerializer();
MyType result = serialzer.Deserialize<MyType>(JsonData);
You can also deserialize an array
您还可以反序列化数组
MyType[] result = serialzer.Deserialize<MyType[]>(JsonData);

