将 JSON 数组反序列化为列表

本文关键字:列表 反序列化 数组 JSON | 更新日期: 2023-09-27 18:36:05

我正在尝试反序列化 json 对象。 它在一定程度上工作正常。我有一个 Area 对象,其中包含一个 Zone 对象数组。

{
  "Area":{
    "id": "0",
    "type": "area",
    "size": {
      "x": 4.5,
      "y": 4.5,
      "z": 4.5
    },
    "position": {
      "x": 0,
      "y": 0,
      "z": 0
    },
    "rotation": {
      "x": 0,
      "y": 0,
      "z": 0
    },
    "zones": [
      {
        "id": "001",
        "type": "zone",
        "size": {
          "x": 1,
          "y": 1,
          "z": 1
        },
        "position": {
          "x": 1,
          "y": 0,
          "z": 1
        },
        "rotation": {
          "x": 0,
          "y": 0,
          "z": 0
        }
      },
      {
        "id": "002",
        "type": "zone",
        "size": {
          "x": 1,
          "y": 1,
          "z": 1
        },
        "position": {
          "x": 3,
          "y": 0,
          "z": 1
        },
        "rotation": {
          "x": 0,
          "y": 0,
          "z": 0
        }
      }
    ]
  }
}

Area类型反序列化很好,但是,我希望区域数组反序列化为List<Zone>. 我怎样才能做到这一点?

string jsonString = r.ReadToEnd();
JavaScriptSerializer jss = new JavaScriptSerializer();
Area area = jss.Deserialize<Area>(jsonString);
public class Area : AirObject3D
{
    public List<Zone> zones;
    public List<TouchPoint> touchPoints;
    public Area()
    {
        this.size.X = 4.5;
        this.size.Z = 4.5;
    }
}

将 JSON 数组反序列化为列表

您只需要正确设置合同即可。为了反序列化列表,您只需将属性类型设置为 List<Zone>(您也可以使用 Zone[] 反序列化为数组)。

例如,您的合同可能如下所示:

public class Root
{
    public Area Area { get; set; }
}
public class Area
{
    [XmlElement("id")]
    public string Id { get; set; }
    [XmlElement("type")]
    public string Type { get; set; }
    [XmlElement("size")]
    public Coordinate Size { get; set; }
    [XmlElement("position")]
    public Coordinate Position { get; set; }
    [XmlElement("rotation")]
    public Coordinate Rotation { get; set; }
    [XmlElement("zones")]
    public List<Zone> Zones { get; set; }
}
public class Zone
{
    [XmlElement("id")]
    public string Id { get; set; }
    [XmlElement("type")]
    public string Type { get; set; }
    [XmlElement("size")]
    public Coordinate Size { get; set; }
    [XmlElement("position")]
    public Coordinate Position { get; set; }
    [XmlElement("rotation")]
    public Coordinate Rotation { get; set; }
}
public class Coordinate
{
    [XmlElement("x")]
    public float X { get; set; }
    [XmlElement("y")]
    public float Y { get; set; }
    [XmlElement("z")]
    public float Z { get; set; }
}

请注意使用 XmlElementAttribute 来确保小写属性正确反序列化为大写的 C# 属性(因为属性应按照约定大写)。

在数据上像这样使用,它可以正确填充所有对象:

JavaScriptSerializer jss = new JavaScriptSerializer();
Root root = jss.Deserialize<Root>(jsonString);

你的对象图应该看起来像 -

public class Size
{
    public double x { get; set; }
    public double y { get; set; }
    public double z { get; set; }
}
public class Position
{
    public int x { get; set; }
    public int y { get; set; }
    public int z { get; set; }
}
public class Rotation
{
    public int x { get; set; }
    public int y { get; set; }
    public int z { get; set; }
}
public class Zone
{
    public string id { get; set; }
    public string type { get; set; }
    public Size size { get; set; }
    public Position position { get; set; }
    public Rotation rotation { get; set; }
}
public class Area
{
    public string id { get; set; }
    public string type { get; set; }
    public Size size { get; set; }
    public Position position { get; set; }
    public Rotation rotation { get; set; }
    public List<Zone> zones { get; set; }
}
public class RootObject
{
    public Area Area { get; set; }
}
var o = JsonConvert.DeserializeObject<RootObject>("json");