0

Okay I have been trying all day long with Regex and some other methods with no luck, here's what i'm trying to do i will try to make it as simple as I can, this an API and I am getting JSON response from my site like this:

{"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",""}}

What i want to extract is user_group_id and secondary_group_ids then parse the numbers and add them to array then compare them with the given number, i want to check if the member is in that group number (secondary or primary does not matter).

How can i do that with the best and easiest way?

ilovebigmacs
  • 983
  • 16
  • 28
Cyb3r
  • 59
  • 9
  • 1
    Duplicate of http://stackoverflow.com/questions/3142495/deserialize-json-into-c-sharp-dynamic-object – Vincent Decaux Jul 06 '15 at 14:57
  • Sorry for that i have read like 50 questions here on Stackoverflow with no luck, let me see that and comment back, thanks. – Cyb3r Jul 06 '15 at 14:58
  • @VincentDecaux possibly, but the better way is to actually define the object as a model and use a deserializer to cast to that object. That way you are not relying on run time dynamic properties possibly existing. – Michael Coxon Jul 06 '15 at 15:00
  • That is invalid JSON. `,""}` <- the "" without key (or value) and the extra closing bracket. – OIS Jul 06 '15 at 15:03
  • @OIS yes I have removed some sensitive data but that's for example. – Cyb3r Jul 06 '15 at 15:08

2 Answers2

1

First, deserialize your JSON string to a .NET Dictionary. For example, with 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

Then you can use basic LINQ queries against the Dictionary.

ilovebigmacs
  • 983
  • 16
  • 28
0

I suggest you use DataContractJsonSerializer to deserialize the Json string into a C# object.

Polyfun
  • 9,479
  • 4
  • 31
  • 39