2013-09-27 1 views
-1

그래서 나는 다음과 같은 JSON 코드가 : C# 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 파트를 지나치지 않았다.

+0

MVC 웹 응용 프로그램 용입니까? JsonFormatter를 사용하여 C# 개체로 변환하고 거기에서 작업 할 것입니다. – Blaise

+1

JSON 구문 분석은 어떻게하고 있습니까? 손으로? .넷 프레임 워크? JSon.NET? –

+0

나는 fiddlercore를 사용하여 json 문자열을 얻고 있습니다. 그것이 내가 위에 게시 한 것입니다. – Skye

답변

2

첫째, 사용자 클래스를 구축 :

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에 올 것이다.

0

사용하여, 다음 (Json2Csharp uisng) ...

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; } 
     } 

을 C#에서 개체를 만들기 Json.NET ...

var json = "your json"; 

var data = JsonConvert.DeserializeObject<RootObject>(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(); 
+0

나는 이것을 시험해 볼 것이다! – Skye

+0

곧 다시보고하게 될 것입니다. – Skye

+0

@ 스케이, 총 보너스를 얻었습니다. 그래서 업데이트했습니다. – christiandev