C#Json – to – Array

所以我有以下json代码:

{ "users":[ { "username":"Deeside", "user_id":750038708, "recent_gifts":[ ], "class_id":2, "bonus":{ "spy_defense":27000, "plunder":172793313, "attack":230400, "defense":153600, "spy_attack":27000 }, "superpower_expire_date":null, "avatar_id":2, "avatar_type":1, "cost":38602761523 }, { "username":"mac78", "user_id":515216476, "recent_gifts":[ ], "class_id":2, "bonus":{ "spy_defense":0, "plunder":172793313, "attack":505800, "defense":336600, "spy_attack":0 }, "superpower_expire_date":null, "avatar_id":2, "avatar_type":2, "cost":38602761523 }, { "username":"divie", "user_id":750047259, "recent_gifts":[ ], "class_id":3, "bonus":{ "spy_defense":62100, "plunder":172793313, "attack":161520, "defense":108720, "spy_attack":54000 }, "superpower_expire_date":null, "avatar_id":3, "avatar_type":1, "cost":38602761512 }, { "username":"xOkSxGoDoFRaNDoM", "user_id":78983234, "recent_gifts":[ ], "class_id":3, "bonus":{ "spy_defense":13500, "plunder":172793313, "attack":270720, "defense":180480, "spy_attack":13500 }, "superpower_expire_date":null, "avatar_id":3, "avatar_type":2, "cost":38602761499 }, { "username":"Polyphanies", "user_id":11876768, "recent_gifts":[ ], "class_id":2, "bonus":{ "spy_defense":25650, "plunder":172793313, "attack":190240, "defense":178740, "spy_attack":5400 }, "superpower_expire_date":null, "avatar_id":2, "avatar_type":2, "cost":38602761489 }, { "username":"l___The_Aces___l", "user_id":1152754333, "recent_gifts":[ ], "class_id":2, "bonus":{ "spy_defense":249300, "plunder":172793313, "attack":0, "defense":0, "spy_attack":249300 }, "superpower_expire_date":null, "avatar_id":2, "avatar_type":1, "cost":38602761468 }, { "username":"amlan", "user_id":1018507644, "recent_gifts":[ ], "class_id":4, "bonus":{ "spy_defense":13500, "plunder":172793313, "attack":131400, "defense":168000, "spy_attack":5400 }, "superpower_expire_date":null, "avatar_id":4, "avatar_type":2, "cost":38602761432 }, { "username":"Astrumstar", "user_id":1521748420, "recent_gifts":[ ], "class_id":3, "bonus":{ "spy_defense":4650, "plunder":172793312, "attack":29900, "defense":105800, "spy_attack":1950 }, "superpower_expire_date":null, "avatar_id":3, "avatar_type":2, "cost":38602761265 }, { "username":"bagarospo", "user_id":2092280432, "recent_gifts":[ ], "class_id":2, "bonus":{ "spy_defense":13500, "plunder":172793312, "attack":241200, "defense":159600, "spy_attack":13500 }, "superpower_expire_date":null, "avatar_id":2, "avatar_type":1, "cost":38602761233 }, { "username":"zpear4", "user_id":380966449, "recent_gifts":[ ], "class_id":3, "bonus":{ "spy_defense":118800, "plunder":172793310, "attack":41274, "defense":40320, "spy_attack":118800 }, "superpower_expire_date":null, "avatar_id":3, "avatar_type":1, "cost":38602760949 }, { "username":"sarrix", "user_id":951332098, "recent_gifts":[ ], "class_id":3, "bonus":{ "spy_defense":150, "plunder":172793303, "attack":2797, "defense":2797, "spy_attack":150 }, "superpower_expire_date":null, "avatar_id":3, "avatar_type":1, "cost":38602759327 }, { "username":"UnsealedExpert", "user_id":1890948836, "recent_gifts":[ ], "class_id":2, "bonus":{ "spy_defense":189900, "plunder":172793295, "attack":0, "defense":0, "spy_attack":189900 }, "superpower_expire_date":null, "avatar_id":2, "avatar_type":2, "cost":38602757394 }, { "username":"______llllllll______", "user_id":783666874, "recent_gifts":[ ], "class_id":2, "bonus":{ "spy_defense":189000, "plunder":172793264, "attack":2400, "defense":1600, "spy_attack":189000 }, "superpower_expire_date":null, "avatar_id":2, "avatar_type":1, "cost":38602750621 }, { "username":"rjh", "user_id":1958058960, "recent_gifts":[ ], "class_id":3, "bonus":{ "spy_defense":29700, "plunder":172792651, "attack":253800, "defense":166200, "spy_attack":29700 }, "superpower_expire_date":null, "avatar_id":3, "avatar_type":2, "cost":38602613553 }, { "username":"Senling", "user_id":1957968694, "recent_gifts":[ ], "class_id":2, "bonus":{ "spy_defense":18900, "plunder":172541048, "attack":111040, "defense":125940, "spy_attack":10800 }, "superpower_expire_date":null, "avatar_id":2, "avatar_type":2, "cost":38546404496 }, { "username":"ee-Rie", "user_id":112693899, "recent_gifts":[ ], "class_id":3, "bonus":{ "spy_defense":29700, "plunder":172541048, "attack":6720, "defense":6720, "spy_attack":29700 }, "superpower_expire_date":null, "avatar_id":3, "avatar_type":1, "cost":38546404367 }, { "username":"JesseJames1986", "user_id":1018522130, "recent_gifts":[ ], "class_id":3, "bonus":{ "spy_defense":0, "plunder":172541047, "attack":98400, "defense":86400, "spy_attack":0 }, "superpower_expire_date":null, "avatar_id":3, "avatar_type":1, "cost":38546404264 }, { "username":"CobraVirus", "user_id":1354087542, "recent_gifts":[ ], "class_id":2, "bonus":{ "spy_defense":2700, "plunder":172541047, "attack":47120, "defense":44745, "spy_attack":2700 }, "superpower_expire_date":null, "avatar_id":2, "avatar_type":1, "cost":38546404120 }, { "username":"TBONE762", "user_id":1219855986, "recent_gifts":[ ], "class_id":2, "bonus":{ "spy_defense":151200, "plunder":172540952, "attack":2400, "defense":1600, "spy_attack":151200 }, "superpower_expire_date":null, "avatar_id":2, "avatar_type":1, "cost":38546382898 }, { "username":"Breaking_Force", "user_id":884257658, "recent_gifts":[ ], "class_id":2, "bonus":{ "spy_defense":421500, "plunder":172482744, "attack":59740, "defense":59740, "spy_attack":421500 }, "superpower_expire_date":null, "avatar_id":2, "avatar_type":1, "cost":38533379145 }, { "username":"llllIllllIllllIllllIllllIlllIl", "user_id":682935410, "recent_gifts":[ ], "class_id":2, "bonus":{ "spy_defense":408000, "plunder":172482744, "attack":87366, "defense":87366, "spy_attack":408000 }, "superpower_expire_date":null, "avatar_id":2, "avatar_type":1, "cost":38533379141 }, { "username":"yenhong11", "user_id":1991515969, "recent_gifts":[ ], "class_id":2, "bonus":{ "spy_defense":45900, "plunder":172482744, "attack":302400, "defense":201600, "spy_attack":45900 }, "superpower_expire_date":null, "avatar_id":2, "avatar_type":1, "cost":38533379134 }, { "username":"-X3X-vv3vv-X3X-", "user_id":1354192816, "recent_gifts":[ ], "class_id":4, "bonus":{ "spy_defense":7650, "plunder":172482744, "attack":28800, "defense":36075, "spy_attack":3600 }, "superpower_expire_date":null, "avatar_id":4, "avatar_type":2, "cost":38533379130 }, { "username":"PrimeMerlinian", "user_id":1186326288, "recent_gifts":[ ], "class_id":4, "bonus":{ "spy_defense":302400, "plunder":172482744, "attack":0, "defense":0, "spy_attack":302400 }, "superpower_expire_date":null, "avatar_id":4, "avatar_type":1, "cost":38533379130 }, { "username":"samiloubra", "user_id":78964704, "recent_gifts":[ ], "class_id":2, "bonus":{ "spy_defense":75900, "plunder":172482744, "attack":346720, "defense":242080, "spy_attack":75900 }, "superpower_expire_date":null, "avatar_id":2, "avatar_type":2, "cost":38533379127 }, { "username":"DARK_Pandora_ANGEL", "user_id":1555414419, "recent_gifts":[ ], "class_id":4, "bonus":{ "spy_defense":218700, "plunder":172482744, "attack":21120, "defense":16320, "spy_attack":218700 }, "superpower_expire_date":null, "avatar_id":4, "avatar_type":1, "cost":38533379127 } ] } 

我如何将它放入C#中的数组中? 然后查找具有最多奖励统计数据的用户名? 我还没有超越json部分,因为在任何地方都会有很multithreading。

首先,构建一个User类:

 public class User { public int AvatarId { get; set; } public int AvatarType { get; set; } public Bonus BonusValues { get; set; } .... } public class Bonus { public int Attack { get; set; } public int Defense { get; set; } .... } 

然后将json字符串反序列化为此对象。

 List users = JsonConvert.DeserializeObject>(json); 

抱歉,我没有时间为您准备一些经过全面测试的代码。 但你应该明白这个想法。 拥有C#对象后,您可以轻松计算总奖励点等。 如果您有任何困难,请告诉我。 今晚我会回到Stackoverflow。

用C#创建对象…(uisng Json2Csharp)

 public class Bonus { public int spy_defense { get; set; } public int plunder { get; set; } public int attack { get; set; } public int defense { get; set; } public int spy_attack { get; set; } public int GetTotalBonus() { return spy_defense + plunder + attack + defense + spy_attack; } } public class User { public string username { get; set; } public int user_id { get; set; } public List recent_gifts { get; set; } public int class_id { get; set; } public Bonus bonus { get; set; } public object superpower_expire_date { get; set; } public int avatar_id { get; set; } public int avatar_type { get; set; } public object cost { get; set; } } public class RootObject { public List users { get; set; } } public class TotalUserBonus { public string username { get; set; } public int totalBonus { get; set; } } 

然后,使用Json.NET …

 var json = "your json"; var data = JsonConvert.DeserializeObject(json); 

要获取具有最多统计数据的用户列表,请按顺序descending

 var totalBonusUsers = (from user in data.users let tot = user.bonus.GetTotalBonus() select new TotalUserBonus {username = user.username, totalBonus = tot}).ToList() .OrderByDescending (u => u.totalBonus).ToList();