通过JavaScriptSerializer解析json字符串

时间:2015-05-25 05:39:01

标签: c# json

我希望JavaScriptSerializer将解析json字符串解析为c#对象,我的json对象如下所示:

[{
            "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"
}]

但是当我声明我的C#对象时如下:

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;}
}

c#编译器告诉我不能声明对象或结构中的检查。 现在,我无法更改从客户端发送的字符串中的checked属性,那么是否有任何解决方案(类似别名)来解决此问题?

由于

3 个答案:

答案 0 :(得分:4)

checked是此处的关键字。显然,它不会接受它作为变种。

尝试使用它:public bool @checked {get;set;}

在变量名称的开头使用@符号,看它是否有效。

答案 1 :(得分:1)

我建议您尝试使用Newtonsoft.Json

你会发现处理你的Jason对象更容易

您的代码将是这样的

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

但首先您需要通过创建名为children的其他类来调整您的类,其中包含从children下的请求中检索到的所有属性,然后调整您的类TreeViewItem以包含{{ 1}}

答案 2 :(得分:0)

@Hussein Khalil - json对象看起来与指定的C#类似。 反序列化可能无法正常工作。