C#的JSON开发包 LitJSON
1 String str = "{’name’:’cyf’,’id’:10,’items’:[{’itemid’:1001,’itemname’:’hello’},{’itemid’:1002,’itemname’:’hello2’}]}";
2 //*** 读取JSON字符串中的数据 *******************************
3 JsonData jd = JsonMapper.ToObject(str);
4 String name = (String)jd["name"];
5 long id = (long)jd["id"];
6 JsonData jdItems = jd["items"];
7 int itemCnt = jdItems.Count;
8 // 数组 items 中项的数量
9 foreach (JsonData item in jdItems)
10 // 遍历数组 items
11 {int itemID = (int)item["itemid"];
12 String itemName = (String)item["itemname"];
13 }
14
15 //*** 将JsonData转换为JSON字符串 ***************************
16
17 String str2 = jd.ToJson();
2 //*** 读取JSON字符串中的数据 *******************************
3 JsonData jd = JsonMapper.ToObject(str);
4 String name = (String)jd["name"];
5 long id = (long)jd["id"];
6 JsonData jdItems = jd["items"];
7 int itemCnt = jdItems.Count;
8 // 数组 items 中项的数量
9 foreach (JsonData item in jdItems)
10 // 遍历数组 items
11 {int itemID = (int)item["itemid"];
12 String itemName = (String)item["itemname"];
13 }
14
15 //*** 将JsonData转换为JSON字符串 ***************************
16
17 String str2 = jd.ToJson();