使用 JSON.NET C# 的内部类的 json 文件进行反序列化



我的json文件中有以下结构

  "missionName": "missionname",
  "thumb_image": "pics/mission-info.png",
   "uplinkPage": [
    {
        "RPC": {
            "name": "RPC",
            "rpc": "",
            "args": ""
        },
        "EXE": {
            "name": "app1",
            "prog": "",
            "args": ""
        },
        "VM": {
            "name": "VM",
            "name": "",
            "args": ""
        },
        "JAR": {
            "name": "JAR",
            "prog": "",
            "args": ""
        },
        "link": {
            "name": "somelink",
            "url": ""
        }
    }
],

为此,我有以下课程

 public class EXE
{
   public string name { get; set; }
   public string prog { get; set; }
   public string args { get; set; }
}
public class RPC
{
    public string name { get; set; }
    public string rpc { get; set; }
    public string args { get; set; }
}
public class VM
{
    public string name { get; set; }
    public string args { get; set; }
}
public class JAR
{
    public string name { get; set; }
    public string prog { get; set; }
    public string args { get; set; }
}
public class Link
{
    public string name { get; set; }
    public string url { get; set; }
}
 public class UplinkPage
{
    public VM[] vmList { get; set; }
    public EXE[] exeList { get; set; }
    public RPC[] rpcList { get; set; }
    public JAR[] jarList { get; set; }
    public Link[] linkList { get; set; }
}
 public class Rootobject
{
    public string missionName { get; set; }
    public string thumb_image { get; set; }     
    public Uplinkpage[] uplinkPage { get; set; }        
}

UplinkPage 部分可以包含一个或多个 EXE、RPC、VM .. 部分。我尝试添加这样的乘法部分

"EXE": {
            "1": {
                "name": "app1",
                "data-prog": "",
                "data-args": ""
            },
            "2": { 
                "name": "app2",
            "data-prog": "",
            "data-args": ""
            }
        }

当我这样进行反序列化时

Rootobject page = JsonConvert.DeserializeObject<Rootobject>(File.ReadAllText("mission1.json"));
我得到

一个对象,我可以读取所有内容,除非我有相同类型的乘法值,我只会得到其中一个。如果我将部分声明为数组

public EXE[] exeList { get; set; }

我会得到最后一个,如果作为

Dictionary<string,EXE> exeList {get; set;}

我会得到第一个。我注意到当我使用字典时,EXE 的类型更改为 EXE1。

所以我想知道我做错了什么?我在这里有什么任务吗?

干杯es

每当有一个可能重复的 JSON 属性时,最简单的方法是将该属性

的值表示为 JSON 数组,而不是具有重复名称的多个属性。 即代替

{
    "EXE" : {"id":1},
    "RPC" : {"name":"a"},        
    "EXE" : {"id":2},
}

你想做:

{
    "EXE" : [{"id":1}, {"id":2}],
    "RPC" : [{"name":"a"}]        
}

同样,在 VM 类中,name出现多次,因此name也应该是一个数组:

public class VM
{
    public string [] name { get; set; }
    public string args { get; set; }
}

(反序列化重复的属性名称并非不可能,只是很困难,并且需要自定义转换器。 请参阅如何反序列化同一对象中具有重复属性名称的 JSON。 由于您可以控制 JSON 格式,因此我建议避免这种情况。 按照您在问题中的建议使用具有索引属性的嵌套对象也是一种选择;难度较小,但仍需要自定义转换。 请参阅如何使用 Newton Soft 为嵌套对象解析此 JSON。 但是使用 JSON 数组是最容易的。

接下来,需要告诉 Json.NET 如何在 c# 属性名称不一致时将 JSON 属性名称映射到 JSON 属性名称。 例如,在 JSON 中,您有一个属性"EXE"但在 c# 中,属性名称为 public EXE[] exeList { get; set; } 。 可以重命名 JSON 属性、重命名 c# 属性或使用 [JsonProperty] 进行映射:

public class UplinkPage
{
    [JsonProperty("VM")]
    public VM[] vmList { get; set; }
    [JsonProperty("EXE")]
    public EXE[] exeList { get; set; }
    [JsonProperty("RPC")]
    public RPC[] rpcList { get; set; }
    [JsonProperty("JAR")]
    public JAR[] jarList { get; set; }
    [JsonProperty("link")]
    public Link[] linkList { get; set; }
}

我还注意到您的EXE对象有时具有"data-prog"属性,有时只是一个"prog"。 你应该标准化一个。

因此,您的 JSON 应如下所示:

{
  "missionName": "missionname",
  "thumb_image": "pics/mission-info.png",
  "uplinkPage": [
    {
      "RPC": [
        {
          "name": "RPC",
          "rpc": "",
          "args": ""
        }
      ],
      "EXE": [
        {
          "name": "app1",
          "prog": "prog1",
          "args": "args1"
        },
        {
          "name": "app2",
          "prog": "prog2",
          "args": "args2"
        }
      ],
      "VM": [
        {
          "name": [
            "VM",
            ""
          ],
          "args": ""
        }
      ],
      "JAR": [
        {
          "name": "JAR",
          "prog": "",
          "args": ""
        }
      ],
      "link": [
        {
          "name": "somelink",
          "url": ""
        }
      ]
    }
  ]
}

您的课程应如下所示:

public class EXE
{
    public string name { get; set; }
    public string prog { get; set; }
    public string args { get; set; }
}
public class RPC
{
    public string name { get; set; }
    public string rpc { get; set; }
    public string args { get; set; }
}
public class VM
{
    public string [] name { get; set; }
    public string args { get; set; }
}
public class JAR
{
    public string name { get; set; }
    public string prog { get; set; }
    public string args { get; set; }
}
public class Link
{
    public string name { get; set; }
    public string url { get; set; }
}
public class UplinkPage
{
    [JsonProperty("VM")]
    public VM[] vmList { get; set; }
    [JsonProperty("EXE")]
    public EXE[] exeList { get; set; }
    [JsonProperty("RPC")]
    public RPC[] rpcList { get; set; }
    [JsonProperty("JAR")]
    public JAR[] jarList { get; set; }
    [JsonProperty("link")]
    public Link[] linkList { get; set; }
}
public class Rootobject
{
    public string missionName { get; set; }
    public string thumb_image { get; set; }
    public UplinkPage[] uplinkPage { get; set; }
}

原型小提琴。

最新更新