簡體   English   中英

使用JSON.NET在C#中反序列化此JSON

[英]Deserializing this JSON in C# with JSON.NET

說我有以下JSON文檔:

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

我無法正常工作。

您在班級成員中有幾種錯別字,但必不可少的部分如下:

看一下JSON文檔的開頭:

{
    "table": [

從本質上講,這意味着您需要一個具有數組屬性的對象,稱為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);
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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM