日期:2014-05-17  浏览次数:20372 次

树形结构的json字符串,如何变成json对象?

{ status:"succeed",msg:"成功",
data:
[
{
"id":"1","name":"根目录","totalCount":12000,
child:
[
{
"id":"137","name":"服饰","totalCount":5000, 
child:
[
{
"id":"140","name":"男装","totalCount":5000, 
child:
[
{
"id":"141","name":"裤子","totalCount":5000
}
]
}
]
}
]
}
]
}


树形结构的json字符串,如何变成json对象?

1、数据如上。
2、不能使用js的eval('('+data+')')。
3、上面是一串字符串,是string类型的,为了方便大家看,加了空格和tab,大家可以当作类似string str='上面的字符串',这个字符串没有空格和tab。
4、要把这串长长的string类型的字符串变成json对象。


------解决方案--------------------
最底层的对象为什么没有child?
------解决方案--------------------
用dynamic去做 如果你是动态的话,
http://blog.csdn.net/educast/article/details/7328106
或者自己构建类去反序列化
------解决方案--------------------
LZ 参考下  这是我的TREE的JSON

  string value = "[{\"id\":0,\"text\":\"自定义组视图\"},{\"id\":-1,\"text\":\"所有测点\",\"iconCls\":\"ico_blank\",\"children\":";//[{\"id\":2,\"text\":\"测点1\"},{\"id\":3,\"text\":\"测点2\"},{\"id\":4,\"text\":\"测点3\"}]}]";
                    string children = "[";
                    for (int a = 0; a < spots.Rows.Count; a++)
                    {
                        if (a != spots.Rows.Count - 1)
                        {
                            children += "{\"id\":" + spots.Rows[a]["id"].ToString() + ",\"text\":\"" + spots.Rows[a]["name"] + "\"},";