C#指定获取Json对象里的指定值 .net framework mvc示例
C#中C#指定获取Json对象里的指定值
https://www.cnblogs.com/sky6699/p/6889762.html
获取的json数据类型样式
{ "status": 0, "msg": "", "data": [ { "id": "63802f4ca7e2fb779a1c3734", "pid": "63773f07d8a85aba9b539f2a", "name": "高三12月听力试音材料.mp3", "type": "audio", "size": 39650244, "create_time": 1669345100, "sample_rate": 44100, "duration": 1239.070125, "bit_rate": 256000, "title": "�����ۺ����������⣨һ��", "artist": "09", "album": "", "track": 0, "channels": 2, "genre": "Blues" }, { "id": "63802ddba7e2fb779a1c36f6", "pid": "63773f07d8a85aba9b539f2a", "name": "gaosan.mp3", "type": "audio", "size": 39650244, "create_time": 1669344731, "sample_rate": 44100, "duration": 1239.070125, "bit_rate": 256000, "title": "�����ۺ����������⣨һ��", "artist": "09", "album": "", "track": 0, "channels": 2, "genre": "Blues" }, { "id": "63802d06a7e2fb779a1c36cd", "pid": "63773f07d8a85aba9b539f2a", "name": "李常超(Lao乾妈) - 一身孤注掷温柔.mp3", "type": "audio", "size": 3357427, "create_time": 1669344518, "sample_rate": 44100, "duration": 206.889796, "bit_rate": 129824, "title": "一身孤注掷温柔", "artist": "李常超(Lao乾妈)", "album": "一身孤注掷温柔", "track": 1, "channels": 2, "genre": "Blues" }, { "id": "63802cf4a7e2fb779a1c36c1", "pid": "63773f07d8a85aba9b539f2a", "name": "汪峰 - 信仰在空中飘扬.mp3", "type": "audio", "size": 7059084, "create_time": 1669344500, "sample_rate": 44100, "duration": 438.465306, "bit_rate": 128796, "title": "信仰在空中飘扬", "artist": "汪峰", "album": "信仰在空中飘扬", "track": 13, "channels": 2, "genre": "Blues" }, { "id": "638027b4d54830242ab7d6c0", "pid": "63773f07d8a85aba9b539f2a", "name": "汪峰11 - 信仰在空中飘扬 - 副本.mp3", "type": "audio", "size": 7059084, "create_time": 1669343156, "sample_rate": 44100, "duration": 438.465306, "bit_rate": 128796, "title": "信仰在空中飘扬", "artist": "汪峰", "album": "信仰在空中飘扬", "track": 13, "channels": 2, "genre": "Blues" } ] }
获取的json数据中data中的指定数据
//获取json指定数据参数 public class getJsonVal { /// <summary> /// T::Json字符串数据::0:0:*:1 /// </summary> public string Jsonstr { get; set; } public string search1{ get; set; } //条件字段参数1 public string search2 { get; set; } //条件字段参数2 public string getsearch1 { get; set; }//取值字段参数1 public string getsearch2 { get; set; }//取值字段参数2 public string searchval1 { get; set; }//条件值1 public string searchval2 { get; set; }//条件值2 }
/// 获取一个json的指定属性值 /// </summary> /// <param name="info">json字符串</param> /// <param name="info1">获取json指定属性值 譬如:取出其中的id</param> /// <param name="info2">获取json指定属性值 譬如:取出其中的name</param> /// <param name="val1">返回数据1</param> /// <param name="val2">返回数据2</param> public static void GetPropertyValue(getJsonVal f, out string returnval1,out string returnval2) { JObject obj = Newtonsoft.Json.Linq.JObject.Parse(f.Jsonstr); var jsobj = obj["data"]; returnval1 = ""; returnval2 = ""; if (!string.IsNullOrEmpty(jsobj.ToString())) { for (var i = 0; i < jsobj.Count(); i++) { if (!string.IsNullOrEmpty(f.search1) && !string.IsNullOrEmpty(f.search2)) { if (jsobj[i][f.search1].ToString() == f.searchval1 && jsobj[i][f.search2].ToString() == f.searchval2) { if (!string.IsNullOrEmpty(f.getsearch1) && !string.IsNullOrEmpty(f.getsearch2)) { returnval1 = jsobj[i][f.getsearch1].ToString(); returnval2 = jsobj[i][f.getsearch2].ToString(); break; } if (!string.IsNullOrEmpty(f.getsearch1)) { returnval1 = jsobj[i][f.getsearch1].ToString(); break; } if (!string.IsNullOrEmpty(f.getsearch2)) { returnval2 = jsobj[i][f.getsearch2].ToString(); break; } } continue; } if (!string.IsNullOrEmpty(f.search1)) { if (jsobj[i][f.search1].ToString() == f.searchval1) { returnval1 = jsobj[i][f.getsearch1].ToString(); break; } continue; } if (!string.IsNullOrEmpty(f.search2)) { if (jsobj[i][f.search2].ToString() == f.searchval2) { returnval2 = jsobj[i][f.getsearch2].ToString(); break; } continue; } } } }
方法使用 --用pid和name做条件,查询获取json串中满足这两个条件的数据id(如果还要获取name,name加一个getsearch2="name")
out返回要的数据(当前方法限定两个,多个的话可自定义加)
本文来自博客园,作者:じ逐梦,转载请注明原文链接:https://www.cnblogs.com/ZhuMeng-Chao/p/16924483.html