我是靠谱客的博主 魁梧冬天,这篇文章主要介绍C#解析JSON,现在分享给大家,希望可以做个参考。

使用开源的类库Newtonsoft.Json(下载地址http://json.codeplex.com/)。下载后添加dll引用就能用。 首先添加引用:using Newtonsoft.Json; 

1.Json字符串普通格式解析(常用)

string jsonText = "{"id":"7788","name":"xxx"}";
JObject jb1 = (JObject)JsonConvert.DeserializeObject(jsonText);
string id = jb1["id"].ToString();
string name = jb1["name"].ToString();

2.Json字符串嵌套格式解析

string jsonText = "{"student":{"name":"张三","spellname":"zhangsan"}}";
JObject jb1 = (JObject)JsonConvert.DeserializeObject(jsonText);
string name = jb1["student"]["name"].ToString();
string spellname = jb1["student"]["spellname"].ToString();

3.Json字符串数组格式解析

string jsonArrayText = "[{"a":"a1","b":"b1"},{"a":"a2","b":"b2"}]";  
JArray jArray = (JArray)JsonConvert.DeserializeObject(jsonArrayText);//jsonArrayText必须是带[]数组格式字符串
string str = jArray[0]["a"].ToString();

最后

以上就是魁梧冬天最近收集整理的关于C#解析JSON的全部内容,更多相关C#解析JSON内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(89)

评论列表共有 0 条评论

立即
投稿
返回
顶部