namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string j = "{success:true,data:{ bin:{code:"JTL-Z38001",name:"奥迪三轮毂",location:"A-001",qty:100}}}";
JObject jo = (JObject)JsonConvertDeserializeObject(j);
ConsoleWriteLine(jo);
}
}
public class Data
{
public Boolean success { get; set; }
public Data1 data { get; set; }
}
public class Data1
{
public Data2 bin { get; set; }
}
public class Data2
{
public string code { get; set; }
public string name { get; set; }
public string location { get; set; }
public Int32 qty { get; set; }
}
}
直接运行,返回结果如下:

如果输出内容修改为:
ConsoleWriteLine(jo["data"]);

继续取bin节点。
ConsoleWriteLine(jo["data"]["bin"]);

最后我们取其中name对应的value。
ConsoleWriteLine(jo["data"]["bin"]["name"]);
一步一步的获取了JSON字符串对应的Value。
——————————————————————————————————————————————————
群里有人提出一个问题,比如我要生成如下的JSON字符串,该如何处理呢?
{
"id": 1,
"value": "cate",
"child": [
{
"id": 1,
"value": "cate",
"child": [
]
},
{
"id": 1,
"value": "cate",
"child": [
{
"id": 2,
"value": "cate2",
"child": [
{
"id": 3,
"value": "cate3",
"child": [
]
}
]
}
]
}
]
}
通过观察我们会发现,其实规律比较好找,就是包含id、value、child这样的属性,child又包含id、value、child这样的属性,可以无限循环下去,是个典型的树形结构。










