1.nuget类库然后引用一下
JObject rss = JObject.Parse(jsonString);
var duoxianOject = (object)rss["questionObj"]["duoxuanList"];
IsoDateTimeConverter timeFormat = new IsoDateTimeConverter();
//将很乱的Json字符格式化 var DuoX = JsonConvert.SerializeObject(duoxianOject, Newtonsoft.Json.Formatting.Indented, timeFormat);
1.新建实体类,将JSon格式字符串解析为C#可以理解的结构
手动新建。或者使用在线生成
注意手动构造实体类的时候一定要注意类型的一致:
Json
{ "id": "137d2415703244f4ba8d222a9eaa7552", "title": "中国特色社会主义制度的基本制度包括", "score": 5, "sscore": 5, "type": "duoxuan", "teacherNote": "", "storeQuestionId": "ff808081719c15a00171a12d4f260310", "loreInfoId": "ff808081719c15cb0171a12cda753e28", "topicItemList": [ { "isAnswer": "false", "index": "A", "content": "人民代表大会制度" }, { "isAnswer": "true", "index": "B", "content": "中国领导下的多党合作和政治协商制度" }, { "isAnswer": "true", "index": "C", "content": "民族区域自治制度" }, { "isAnswer": "true", "index": "D", "content": "基层群众自治制度" } ], "sanswer": "B|C|D", "optionList": [ { "isAnswer": false, "index": "A", "content": "人民代表大会制度", "indexName": "A", "title": "人民代表大会制度" }, { "isAnswer": true, "index": "B", "content": "中国领导下的多党合作和政治协商制度", "indexName": "B", "title": "中国领导下的多党合作和政治协商制度" }, { "isAnswer": true, "index": "C", "content": "民族区域自治制度", "indexName": "C", "title": "民族区域自治制度" }, { "isAnswer": true, "index": "D", "content": "基层群众自治制度", "indexName": "D", "title": "基层群众自治制度" } ] }
实体类
public class TopicItemListItem { /// <summary> /// /// </summary> public string isAnswer { get; set; } /// <summary> /// /// </summary> public string index { get; set; } /// <summary> /// 人民代表大会制度 /// </summary> public string content { get; set; } } public class OptionListItem { /// <summary> /// /// </summary> public string isAnswer { get; set; } /// <summary> /// /// </summary> public string index { get; set; } /// <summary> /// 人民代表大会制度 /// </summary> public string content { get; set; } /// <summary> /// /// </summary> public string indexName { get; set; } /// <summary> /// 人民代表大会制度 /// </summary> public string title { get; set; } } public class AnswerMoudel { /// <summary> /// /// </summary> public string id { get; set; } /// <summary> /// 中国特色社会主义制度的基本制度包括 /// </summary> public string title { get; set; } /// <summary> /// /// </summary> public int score { get; set; } /// <summary> /// /// </summary> public int sscore { get; set; } /// <summary> /// /// </summary> public string type { get; set; } /// <summary> /// /// </summary> public string teacherNote { get; set; } /// <summary> /// /// </summary> public string storeQuestionId { get; set; } /// <summary> /// /// </summary> public string loreInfoId { get; set; } /// <summary> /// /// </summary> public List<TopicItemListItem> topicItemList { get; set; } /// <summary> /// /// </summary> public string sanswer { get; set; } /// <summary> /// /// </summary> public List<OptionListItem> optionList { get; set; }
}
构造完毕之后,将Json数据按照上面这种结构存到泛型中
//有很多JSON数据就用List
<List<AnswerMoudel>> danxuan = JsonConvert.DeserializeObject<List<AnswerMoudel>>(DanX);
//单条
AnswerMoudel danxuan = JsonConvert.DeserializeObject<AnswerMoudel>(DanX);
原文:https://www.cnblogs.com/tangpeng97/p/12822442.html