简体   繁体   中英

JSON stringified array to mvc 4 c# array

I'm new to mvc 4 and JSON and i'm having some problems. I have this JSON stringified array:

[{"id":2,"aantal":4},{"id":4,"aantal":1}]

I want to parse this to a mvc 4 c# array or 2d array. I have been searching for hours and i couldn't find any solution. How can i achieve that?

Thanking in advance.

It's not a 2d array so you probably will never have much luck with that. Here is the simplest way of doing it using json.NET.

//in the file where you use JsonConvert
using Newtonsoft.Json;

public class item
{
    public int id { get; set; }
    public int aantal { get; set; }
}


item[] myItems = JsonConvert.Deserialize<item[]>(jsonString);

If you go this route the key is just identifying how your json structures equate to C# objects/collections/collections of collections of objects ect. There are more dynamic/less type safe ways of doing this that don't require you to define objects to deserialize into, it's my personal preference to use this style whenever possible. If you're not already using json.NET you can get the package via nuget or here http://james.newtonking.com/json

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