-1

所以我有以下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 部分,因为到处都有线程分配。

4

3 回答 3

2

首先,构建一个 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<User> users = JsonConvert.DeserializeObject<List<User>>(json);

抱歉,我没有时间为您准备一些经过全面测试的代码。但你应该明白这一点。一旦你有了 C# 对象,你就可以很容易地计算总奖励点等等。如果您有任何困难,请告诉我。今晚我将回到 Stackoverflow。

于 2013-09-27T21:30:49.513 回答
0

Query using Linq to JSON. Here is the documention.http://james.newtonking.com/projects/json/help/index.html?topic=html/QueryingLINQtoJSON.htm

于 2013-09-27T21:33:38.480 回答
0

Create the objects in 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<object> 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<User> users { get; set; }
}

public class TotalUserBonus
        {
            public string username { get; set; }
            public int totalBonus { get; set; }
        }

Then, using Json.NET...

var json = "your json";

var data = JsonConvert.DeserializeObject<RootObject>(json);

To get a list of the users with most stats, ordered 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();
于 2013-09-27T21:34:52.530 回答