简体   繁体   中英

Deserialize JSON object containing array to C# object

This is my JSON string:

{"type":"motor","ids":["1","2","5","7","8","10"]}

And this is the object I want to generate from it:

public class ElementArray {
    public ElementType type;
    public String[] ids;

    public ElementArray() {
    }
}

How can I achieve that? I googled about Json.NET, but they only explain how to deserialize an array but not how to deserialize an object containing an array as a field (see my class above).

What I tried is

JavaScriptSerializer jss = new JavaScriptSerializer();
ElementArray elements = jss.Deserialize<ElementArray>(strJson);

but when I debug the code, the field ids contains null .

IMO simplest way to deal with json is using newtonsoft.json library

http://www.newtonsoft.com/json

And here is example how to deserialize object:

http://www.newtonsoft.com/json/help/html/DeserializeObject.htm

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