C# class 为 JSON 数据结构设计

C# class design for JSON data structure

我正在编写一些代码以满足政府部门的规范。我无法在 C#

中将部分解析为 类

样本

{
    "tdeVersion": "2.0",
    "batchId": "ABC20190803",
    "deviceRecords": [
        {
            "device": {
                "id": "1234"
            },
            "records": [
                {
                    "type": "POSITION",
                    "number": 1234,
                    "dateTime": "2019-01-23T03:04:05Z",
                    "position": {
                        "latitude": -36.00001,
                        "longitude": 140
                    },
                    "speed": 98.7,
                    "receiptDateTime": "2019-01-23T03:04:05Z"
                }
            ]
        }
    ]
}

我的问题是 deviceRecords 集合元素。通常我会使用类似 List 的东西。我应该在这里使用什么?我已经将其余部分映射到 类.

您可以尝试将内容解析为“JObject”而不是List,这样比List更容易使用,然后您可以调用like

JObject Res = JObject.Parse(jsondata);
Response.Write(Res["deviceRecords"][0]["device"]["id"]);

或者如果你不想使用索引,你可以使用 foreach 循环 Res["deviceRecords"].

你有这个 JSON:

{
    "tdeVersion": "2.0",
    "batchId": "ABC20190803",
    "deviceRecords": [
        {
            "device": {
                "id": "1234"
            },
            "records": [
                {
                    "type": "POSITION",
                    "number": 1234,
                    "dateTime": "2019-01-23T03:04:05Z",
                    "position": {
                        "latitude": -36.00001,
                        "longitude": 140
                    },
                    "speed": 98.7,
                    "receiptDateTime": "2019-01-23T03:04:05Z"
                }
            ]
        }
    ]
}

让我们首先创建 inner-most 对象:位置。

public class Position
{
    public double Latitude { get; set; }
    public double Longitude { get; set; }
}

然后我们可以创建包含它的记录对象:

public class RecordEntity // record is a keyword in C#, so we'll call this RecordEntity
{
    public string Type { get; set; }
    public int Number { get; set; }
    [JsonProperty("dateTime")]
    public DateTime DateAndTime { get; set; } // DateTime is a keyword
    public double Speed { get; set; }
    public DateTime ReceiptDateAndTime { get; set; }
}

然后是设备对象:

public class Device
{
    public string Id { get; set; }
}

接下来我们可以制作包含Device和RecordEntity的对象:

public class DeviceRecords
{
    public Device Device { get; set; }
    // records is an array so we need a collection type in C#
    public List<RecordEntity> Records { get; set; }
}

然后我们需要创建 outer-most 对象来容纳这些:

public class Batch
{
    public string TdeVersion { get; set; }
    public string BatchId { get; set; }
    [JsonProperty("deviceRecords")]
    public List<DeviceRecords> DevicesWithRecords { get; set; } // deviceRecords seemed confusing
}

现在我们应该能够反序列化为 Batch:

string json = "{\"tdeVersion\":\"2.0\",\"batchId\":\"ABC20190803\",\"deviceRecords\":[{\"device\":{\"id\":\"1234\"},\"records\":[{\"type\":\"POSITION\",\"number\":1234,\"dateTime\":\"2019-01-23T03:04:05Z\",\"position\":{\"latitude\":-36.00001,\"longitude\":140},\"speed\":98.7,\"receiptDateTime\":\"2019-01-23T03:04:05Z\"}]}]}";

// if you're using JSON.NET:
Batch batch = JsonConvert.DeserializeObject<Batch>(json);

// if you're using System.Text.Json:
Batch batch = JsonSerializer.Deserialize<Batch>(json);