2017-07-15 130 views
0

我有從另一個應用程序返回的json對象,我無法控制它,每個對象的結構都不同,但我想從每個對象中提取相同的數據與它的標題(我是使用NewtownSoft):在c中反序列化Json對象#

{ 
"myData": [ 
{ 
    "one": { 
    "in": 0, 
    "out": 17, 
    "total": 17 
    }, 
    "two": { 
    "total": 17 
    }, 
    "three": { 
    "total": 0 
    }, 
    "four": { 
    "total": 8 
    }, 
    "five": { 
    "total": 0 
    }, 
    "six": { 
    "total": 0 
    }, 
    "seven": { 
    "total": 0 
    } 
} ]} 

我想要得到的結果是,因爲這圖像

enter image description here

在並且僅使用一類

反序列化此代碼
public class Example{ 
public string number {get;set;} 
public int total {get; set;} 
} 
+4

[用C#反序列化JSON]的可能的複製(https://stackoverflow.com/questions/7895105/deserialize-json-with-c-sharp) –

回答

0

如果你有超過所產生的JSON控制,然後修改JSON:

{ 
"myData": [ 
{ 
    "Example": { 
    "number": "one", 
    "in": 0, 
    "out": 17, 
    "total": 17 
    }, 
    "Example": { 
    "number": "two", 
    "total": 17 
    }, 
    "Example": { 
    "number": "three", 
    "total": 0 
    }, 
    "Example": { 
    "number": "four", 
    "total": 8 
    }, 
    "Example": { 
    "number": "five", 
    "total": 0 
    }, 
    "Example": { 
    "number": "six", 
    "total": 0 
    }, 
    "Example": { 
    "number": "seven", 
    "total": 0 
    } 
} ]} 

C#類:

public class Example 
{ 
    public string number { get; set; } 
    public int total { get; set; } 
} 

public class MyData 
{ 
    public Example Example { get; set; } 
} 

public class RootObject 
{ 
    public List<MyData> myData { get; set; } 
} 
+0

我無法控制它 –