2016-06-18 4 views
1
와 C#에서이 JSON를 직렬화 복원

내가 다음 JSON 문서 말해봐 :는 JSON.NET

{ 
    "table": [ 
     { 
      "wiki": "http://en.wikipedia.org/wiki/Period%201%20element", 
      "elements": [ 
       { 
        "group": "", 
        "position": 0, 
        "name": "Hydrogen", 
        "number": 1, 
        "small": "H", 
        "molar": 1.00794, 
        "electrons": [ 
         1 
        ] 
       }, 
       { 
        "group": "Element Noble p", 
        "position": 17, 
        "name": "Helium", 
        "number": 2, 
        "small": "He", 
        "molar": 4.002602, 
        "electrons": [ 
         2 
        ] 
       } 
      ] 
     }, 
     { 
      "wiki": "http://en.wikipedia.org/wiki/Period%202%20element", 
      "elements": [ 
       { 
        "group": "Element Alkali s", 
        "position": 0, 
        "name": "Lithium", 
        "number": 3, 
        "small": "Li", 
        "molar": 6.941, 
        "electrons": [ 
         2, 
         1 
        ] 
       }, 
       { 
        "group": "Element Alkaline s", 
        "position": 1, 
        "name": "Beryllium", 
        "number": 4, 
        "small": "Be", 
        "molar": 9.012182, 
        "electrons": [ 
         2, 
         2 
        ] 
       }, 
       { 
        "group": "Element Metalloid Boron p", 
        "position": 12, 
        "name": "Boron", 
        "number": 5, 
        "small": "B", 
        "molar": 10.811, 
        "electrons": [ 
         2, 
         3 
        ] 
       }, 
       { 
        "group": "Element Nonmetal Carbon p", 
        "position": 13, 
        "name": "Carbon", 
        "number": 6, 
        "small": "C", 
        "molar": 12.0107, 
        "electrons": [ 
         2, 
         4 
        ] 
       }, 
       { 
        "group": "Element Nonmetal Pnictogen p", 
        "position": 14, 
        "name": "Nitrogen", 
        "number": 7, 
        "small": "N", 
        "molar": 14.0067, 
        "electrons": [ 
         2, 
         5 
        ] 
       }, 
       { 
        "group": "Element Nonmetal Chalcogen p", 
        "position": 15, 
        "name": "Oxygen", 
        "number": 8, 
        "small": "O", 
        "molar": 15.9994, 
        "electrons": [ 
         2, 
         6 
        ] 
       }, 
       { 
        "group": "Element Halogen p", 
        "position": 16, 
        "name": "Fluorine", 
        "number": 9, 
        "small": "F", 
        "molar": 18.998404, 
        "electrons": [ 
         2, 
         7 
        ] 
       }, 
       { 
        "group": "Element Noble p", 
        "position": 17, 
        "name": "Neon", 
        "number": 10, 
        "small": "Ne", 
        "molar": 20.1797, 
        "electrons": [ 
         2, 
         8 
        ] 
       } 
      ] 
     } 

을 ... 그리고 내가이 JSON으로 직렬화 어떻게 ... 계속적으로

간다 Element라는 클래스? 원하는 경우 "wiki"필드를 무시할 수 있습니다.

은 내가 시도하는 것 :

Table t = JsonConvert.DeserializeObject<Table> (lines); 

class Table { 
    public string wiki; 
    public Element[] element; 
} 

class Element { 
    public string group, name, small; 
    public int position, numer; 
    public double molar; 
    public int[] electrons; 
} 

나는 그것이 동작하지 않습니다.

답변

0
Parent = JsonConvert.DeserializeObject<Parent> (lines); 

class Parent { 
    public Mid[] table; 
} 

class Mid { 
    public string wiki; 
    public Element[] elements; 
} 

class Element { 
    public string group, name, small; 
    public int position, numer; 
    public double molar; 
    public int[] electrons; 
} 
2

당신은 클래스 멤버의 여러 오타가 있지만 다음과 같이 필수적인 부분입니다

{ 
    "table": [ 

그것은 본질적 것을 의미한다 :

이 JSON 문서의 시작 부분에보세요 배열 속성이 table 인 개체가 필요합니다.

올바른 클래스는 다음과 같습니다

class Root 
{ 
    public Table[] table; 
} 

class Table 
{ 
    public string wiki; 
    public Element[] elements; 
} 

class Element 
{ 
    public string group, name, small; 
    public int position, number; 
    public double molar; 
    public int[] electrons; 
} 

및 역 직렬화 코드 :

var root = JsonConvert.DeserializeObject<Root>(lines);