简体   繁体   English

在C#中反序列化Json对象

[英]Deserialize Json objects in c#

I have json object returned from another application, that i have no control over it and the structure of each object is different ,but i want to extract the same data from each object with its title (I am using NewtownSoft): 我有另一个应用程序返回的json对象,我无法控制它,并且每个对象的结构都不同,但是我想从每个对象中提取相同的数据及其标题(我正在使用NewtownSoft):

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

i want the result to be as in this image 我希望结果像这张图片一样

在此处输入图片说明

and deserialize this code using only one class 并仅使用一个类反序列化此代码

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

If you have control over the JSON that is generated, then modify the JSON: 如果您可以控制生成的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# Classes: 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; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM