简体   繁体   中英

Is there a way to pass a list of JSON objects from JS to C#?

Something I'm confusing. The Javascript is going to produce the following JSON data.

{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.

Thanks for your answer or hints.

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.

What I would actually recommend is sending over valid JSON instead. You can accomplish this by doing something like this:

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

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.

Once you pass the above JSON to C#, you can deserialize it by doing something like this:

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#:

Dictionary<string, object> result =>
    { "list" => object },
                object => List<object>,
                          List<object> => Dictionary<string, object>
                                          { "type" => "book", "author" => "Lian" } // etc
[
    {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.

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.

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 serializer = new JavaScriptSerializer();
MyType result = serialzer.Deserialize<MyType>(JsonData);

You can also deserialize an array

MyType[] result = serialzer.Deserialize<MyType[]>(JsonData);

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