2014-06-19 6 views
0

개체를 deserialize하려고 할 때 형식에 채워지지만 List<Sport>에 null이 나타납니다. 어떤 아이디어?일반 목록으로 개체를 비 직렬화

내 클래스 : 직렬화 복원

class Sports 
{ 
    public MsgTypes type { get; set; } 
    public List<Sport> Sport { get; set; } 
} 

class Sport 
{ 
    public int Id { get; set; } 
    public int Import_id { get; set; } 
    public string Name { get; set; } 
    public int Active { get; set; } 
    public int Order { get; set; } 
    public int Min_bet { get; set; } 
    public int Max_bet { get; set; } 
    public int Updated { get; set; } 
    public string Feed_type { get; set; } 
    public string Locale { get; set; } 
} 

명령은 : 당신은 중첩 및 다른 클래스 이름을 하나 더 수준이 필요

"{\"code\":0,\"type\":4,\"Sports\":[{\"Sport\":{\"id\":\"1\",\"import_id\":\"1\",\"name\":\"Soccer\",\"active\":true,\"order\":\"1\",\"min_bet\":\"0\",\"max_bet\":\"0\",\"updated\":\"1403194889\",\"feed_type\":\"Betradar\",\"locale\":\"en_us\"}},{\"Sport\":{\"id\":\"2\",\"import_id\":\"5\",\"name\":\"Tennis\",\"active\":true,\"order\":\"3\",\"min_bet\":\"0\",\"max_bet\":\"0\",\"updated\":\"1403194771\",\"feed_type\":\"Betradar\",\"locale\":\"en_us\"}},{\"Sport\":{\"id\":\"3\",\"import_id\":\"6\",\"name\":\"Handball\",\"active\":true,\"order\":\"6\",\"min_bet\":\"0\",\"max_bet\":\"0\",\"updated\":\"1403152901\",\"feed_type\":\"Betradar\",\"locale\":\"en_us\"}},{\"Sport\":{\"id\":\"4\",\"import_id\":\"4\",\"name\":\"Ice Hockey\",\"active\":true,\"order\":\"4\",\"min_bet\":\"0\",\"max_bet\":\"0\",\"updated\":\"1403080245\",\"feed_type\":\"Betradar\",\"locale\":\"en_us\"}},{\"Sport\":{\"id\":\"7\",\"import_id\":\"2\",\"name\":\"Basketball\",\"active\":true,\"order\":\"2\",\"min_bet\":\"0\",\"max_bet\":\"0\",\"updated\":\"1403194830\",\"feed_type\":\"Betradar\",\"locale\":\"en_us\"}},{\"Sport\":{\"id\":\"8\",\"import_id\":\"23\",\"name\":\"Volleyball\",\"active\":true,\"order\":\"5\",\"min_bet\":\"0\",\"max_bet\":\"0\",\"updated\":\"1403194591\",\"feed_type\":\"Betradar\",\"locale\":\"en_us\"}},{\"Sport\":{\"id\":\"9\",\"import_id\":\"12\",\"name\":\"Rugby\",\"active\":true,\"order\":\"7\",\"min_bet\":\"0\",\"max_bet\":\"0\",\"updated\":\"1403194710\",\"feed_type\":\"Betradar\",\"locale\":\"en_us\"}},{\"Sport\":{\"id\":\"12\",\"import_id\":\"11\",\"name\":\"Motorsport\",\"active\":true,\"order\":\"12\",\"min_bet\":\"0\",\"max_bet\":\"0\",\"updated\":\"1403065699\",\"feed_type\":\"Betradar\",\"locale\":\"en_us\"}},{\"Sport\":{\"id\":\"13\",\"import_id\":\"3\",\"name\":\"Baseball\",\"active\":true,\"order\":\"13\",\"min_bet\":\"0\",\"max_bet\":\"0\",\"updated\":\"1403194834\",\"feed_type\":\"Betradar\",\"locale\":\"en_us\"}},{\"Sport\":{\"id\":\"14\",\"import_id\":\"16\",\"name\":\"American Football\",\"active\":true,\"order\":\"14\",\"min_bet\":\"0\",\"max_bet\":\"0\",\"updated\":\"1403143326\",\"feed_type\":\"Betradar\",\"locale\":\"en_us\"}},{\"Sport\":{\"id\":\"16\",\"import_id\":\"34\",\"name\":\"Beach Volley\",\"active\":true,\"order\":\"16\",\"min_bet\":\"0\",\"max_bet\":\"0\",\"updated\":\"1403194417\",\"feed_type\":\"Betradar\",\"locale\":\"en_us\"}}]}" 
+0

가 보이는이 JSON이 유효하지 않습니다처럼. – Anuraj

+1

'스포츠'의'List '는'스포츠'가 아닌'스포츠'라는 이름이되어야한다고 생각합니다 –

+0

@ChrisMantie : 클래스 이름은이 클래스의 속성 이름과 같을 수 없습니다;) 스포츠 클래스는 꽤 ​​이상하게 보입니다 조금도. 다른 엔터티가 컬렉션을 가질 것으로 예상됩니다. 스포츠 란 무엇을 의미합니까? –

답변

0

:

Sports _sports = (Sports) JsonConvert.DeserializeObject<Sports>(jsonObj); 

이 내 JSON 객체입니다 . 당신은 이러한 구조에 역 직렬화 할 수 있어야한다 :

class SportsParent 
{ 
    //Code for MsgTypes was not provided so it is commented out 
    public List<SportGroup> Sports { get; set; } 
} 

class SportGroup 
{ 
    public SportItem Sport { get; set; } 
} 

class SportItem 
{ 
    public int Id { get; set; } 
    public int Import_id { get; set; } 
    public string Name { get; set; } 
    public bool Active { get; set; } //need to be converted to bool instead of int 
    public int Order { get; set; } 
    public int Min_bet { get; set; } 
    public int Max_bet { get; set; } 
    public int Updated { get; set; } 
    public string Feed_type { get; set; } 
    public string Locale { get; set; } 
} 

당신은 코드를 사용하여 역 직렬화 할 수 있습니다

SportsParent _sports = JsonConvert.DeserializeObject<SportsParent>(jsonObj); 
+0

답장을 보내 주셔서 감사합니다. 실제로 작동했습니다 :) 그러나 중첩의 추가 수준이 필요한 이유를 알지 못합니다. 설명을 해 주시면 친절하니? – user3757324

+0

직렬화 된 json의 트림 된 버전은 { "Sports": [{ "Sport": { "id": "1",}}]}처럼 보입니다. 즉, Sports라는 목록 속성이있는 개체가 있음을 의미합니다 (SportsParent 클래스 및 Sports 속성으로 나타남). 배열의 각 항목은 Sport라는 단일 속성 (SportGroup 클래스의 코드에서)을 나타내는 { "Sport": { "id": "1",}}로 표시되며 실제 데이터는 해당 객체 SportItem). 나는 그것이 의미가 있기를 바랍니다. – dotnetom

+0

예, 감사합니다. – user3757324