好吧,我一直在尝试使用正则表达式以及其他一些没有运气的方法,这就是我想要做的事情,我会尝试尽可能简单,这是一个API我正在从我的网站获得JSON响应:
{"user_id":1,"username":"xxx","email":"xxx@xxx.com","gender":"male","title":"","language_id":1,"timezone":"Africa\/Nairobi","visible":1,"activity_visible":1,"user_group_id":3,"secondary_group_ids":"2,4,6,8,11,12,13,16,19,20","message_count":235,"conversations_unread":0,"register_date":1424485355,"last_activity":1436186781,"trophy_points":43,"alerts_unread":2,"avatar_date":1435657653,"avatar_width":180,"avatar_height":224,"gravatar":"","user_state":"valid","is_moderator":0,"is_admin":1,"is_banned":0,"like_count":127,"warning_points":0,"is_staff":1,"advapps":"a:1:{i:0;a:2:{s:5:\"posid\";i:1;s:5:\"count\";i:5;}}","brms_statistic_perferences":"a:1:{i:1;s:1:\"0\";}","bratr_ratings":38,"tc_cui_icon":"","tc_cui_color":"#000000","breta_user_level":5,"breta_curent_level":34,"breta_next_level":45,"credits":"13402154377.480000","brc_points":"999999.000000","br_profile_image":"","br_cropy":"0.00",""}}
我要提取的是user_group_id
和secondary_group_ids
然后解析数字并将它们添加到数组然后将它们与给定的数字进行比较,我想检查该成员是否在该组编号中(中学或小学无关紧要)。
我怎样才能以最好最简单的方式做到这一点?
答案 0 :(得分:1)
首先,将您的JSON字符串反序列化为.NET字典。例如,使用JSON.NET:
string json = @"[
{
'Name': 'Product 1',
'ExpiryDate': '2000-12-29T00:00Z',
'Price': 99.95,
'Sizes': null
},
{
'Name': 'Product 2',
'ExpiryDate': '2009-07-31T00:00Z',
'Price': 12.50,
'Sizes': null
}
]";
List<Product> products = JsonConvert.DeserializeObject<List<Product>>(json);
Console.WriteLine(products.Count);
// 2
Product p1 = products[0];
Console.WriteLine(p1.Name);
// Product 1
然后,您可以对字典使用基本的LINQ查询。
答案 1 :(得分:0)
我建议你使用DataContractJsonSerializer将Json字符串反序列化为C#对象。