C# 解析JSON -- Newtonsoft.Json


1:Newtonsoft.Json.dll 下载  http://json.codeplex.com/

2:解析JSON字符串

            StringBuilder sb = new StringBuilder();
            string strs = "[{ID:'1',Name:'John',Other:[{Age:'21',Sex:'0'}]},{ID:'2',Name:'Good',Other:[{Age:'36',Sex:'1'}]}]";  
            JArray javascript = (JArray)JsonConvert.DeserializeObject(strs);
            for (int i = 0; i < javascript.Count; i++)
            {
                JObject obj = (JObject)javascript[i];
                sb.Append("ID:" + obj["ID"].ToString());
                sb.Append("Name:" + obj["Name"].ToString());
                JArray json = (JArray)obj["Other"];
                for (int j = 0; j < json.Count; j++)
                {
                    JObject jsonobj = (JObject)json[j];
                    sb.Append("Age:" + jsonobj["Age"].ToString());
                    sb.Append("Sex:" + jsonobj["Sex"].ToString());
                }
            }

你可能感兴趣的:(C#)