简体   繁体   中英

Parse json string by JavaScriptSerializer

I want parse json string into c# object by JavaScriptSerializer, my json object looks like below:

[{
            "text" : "aaaa",
            "cls" : "folder",
            "checked" : false,
            "isconsign" : false,
            "myid" : "catalog_1",
            "id" : "catalog_1",
            "children" : [{
                "text" : "vvvv",
                "cls" : "file",
                "leaf" : true,
                "checked" : false,
                "disabled" : true,
                "myid" : "4456",
                "id" : "input_4456"
            }, {
                "text" : "sdf",
                "cls" : "file",
                "leaf" : true,
                "checked" : false,
                "disabled" : true,
                "myid" : "4465",
                "id" : "input_4465"
}]

But when I declare my C# object like below:

public class TreeViewItem
{
    public string text {get;set;}
    public string cls {get;set;}
    public bool disabled {get;set;}
    public bool isconsign {get;set;}
    public bool leaf {get;set;}
    public string myid {get;set;}
    public string id {get;set;}
    public bool checked {get;set;}
}

The c# compiler tell me can not declare checked in object or struct. Now, I can't change the checked property in the string send from client, so is there any solution (something like alias) to resolve this problem?

Thanks

checked is a keyword here. Obviously, its not going to accept it as a varaible.

Try using it like this : public bool @checked {get;set;}

Use that @ sign just at the start of your variable name and see if it works.

I recommend that you try using Newtonsoft.Json

you will find that it is easier to handle your Jason objects

your code will be like this

TreeViewItem defaultCallResult = JsonConvert.DeserializeObject<TreeViewItem>(return_value);

but first you need to adjust your Classes by creating other class named children holding all the properties retrieved form your request under children then adjust your class TreeViewItem to contain list of children

@Hussein Khalil - The json object doesn't look similar to the C# type specified. The deserialization might not work properly.

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