将JSON结果反序列化为C#对象

b4qexyjb  于 2023-05-23  发布在  C#
关注(0)|答案(4)|浏览(159)

我有这个JSON结果,我从API收到有一个简单的方法来获取所需的值从响应?只需要将这些值AssetName、SystemID、Lat、Lng和Timestmp放入名为Result的自定义类中
结果Json如下所示

{
    "result": [
        {
            "Asset": {
                "AssetName": "Banner",
                "AssetType": "Medic",
                "AssetCategory": "Personnel",
                "Hardware": {
                    "HardwareName": " Tag 88",
                    "HardwareType": "Personnel Tag",
                    "HardwareFamily": " Group 2",
                    "DeviceType": "Location Tag",
                    "SystemId": "8845"
                }
            },
            "DetectingAsset": {
                "AssetName": "Shop 2 TR",
                "AssetType": "Tag Reader",
                "AssetCategory": "Network Access Point",
                "Hardware": {
                    "HardwareName": "2 TR",
                    "HardwareType": "Tag Reader",
                    "HardwareFamily": "Group 2",
                    "DeviceType": "Tag Reader",
                    "SystemId": "9.9.1"
                }
            },
            "Location": {
                "Maps": [
                    "Hard Rock"
                ],
                "Zone": "Machine Shop",
                "Lat": null,
                "Lng": null
            },
            "Timestamp": "2023-05-05T11:07:45.673"
        },
        {
            "Asset": {
                "AssetName": "Test1",
                "AssetType": "Medic",
                "AssetCategory": "Personnel",
                "Hardware": {
                    "HardwareName": "Tag 88",
                    "HardwareType": "Personnel Tag",
                    "HardwareFamily": "Group ABIP",
                    "DeviceType": "Location Tag",
                    "SystemId": "1022"
                }
            },
            "DetectingAsset": {
                "AssetName": "Machine Shop 2 TR",
                "AssetType": "Tag Reader",
                "AssetCategory": "Network Access Point",
                "Hardware": {
                    "HardwareName": "2 TR",
                    "HardwareType": "Tag Reader",
                    "HardwareFamily": "Group AIP",
                    "DeviceType": "Tag Reader",
                    "SystemId": "9.9.1"
                }
            },
            "Location": {
                "Maps": [
                    "Hard Rock"
                ],
                "Zone": "Machine Shop",
                "Lat": null,
                "Lng": null
            },
            "Timestamp": "2023-05-05T11:07:45.673"
        }
    ]
        }

我创建的类如下

class Result
    {
        public APIResult[] aPIResult { get; set; }
    }

    class Result
    {
        public string TimeStamp { get; set; } 
        public string AssetName { get; set; }
        public string locationLat { get; set; }
        public string locationLng { get; set; }
        public string SystemId {get;set;}

    }
bmvo0sr5

bmvo0sr51#

您可以使用Newtonsoft.Json(Json.NET)库中的JObject类来解析并从JSON响应中提取所需的值。

using Newtonsoft.Json.Linq;
using System;

class Program
{
    static void Main()
    {
        string json = "Your JSON string";

        JObject jsonObject = JObject.Parse(json);
        JArray resultsArray = (JArray)jsonObject["result"];

        foreach (JToken resultToken in resultsArray)
        {
            Result result = new Result();
            
            result.AssetName = (string)resultToken["Asset"]["AssetName"];
            result.SystemId = (string)resultToken["Asset"]["Hardware"]["SystemId"];
            result.locationLat = (string)resultToken["Location"]["Lat"];
            result.locationLng = (string)resultToken["Location"]["Lng"];
            result.TimeStamp = (string)resultToken["Timestamp"];

            Console.WriteLine($"AssetName: {result.AssetName}");
            Console.WriteLine($"SystemID: {result.SystemId}");
            Console.WriteLine($"Lat: {result.locationLat}");
            Console.WriteLine($"Lng: {result.locationLng}");
            Console.WriteLine($"Timestmp: {result.TimeStamp}");
            Console.WriteLine();
        }
    }
}

class Result
{
    public string TimeStamp { get; set; }
    public string AssetName { get; set; }
    public string locationLat { get; set; }
    public string locationLng { get; set; }
    public string SystemId { get; set; }
}
0kjbasz6

0kjbasz62#

可以使用Newtonsoft.Json.JsonConvert

ObjectType object = JsonConvert.DeserializeObject<ObjectType>(jsonString);
ycggw6v2

ycggw6v23#

您可以使用System.Text.Json来实现此功能(可通过Visual Studio中的Nuget Package Manager安装):

Result result = JsonSerializer.Deserialize<Result>(jsonDataString);
jdgnovmf

jdgnovmf4#

你可以试试这个代码

Data data = JsonConvert.DeserializeObject<Data>(json);

public class Data
{
    public List<Result> result { get; set; }
}

public class Result
{
    public Asset Asset { get; set; }
    public Asset DetectingAsset { get; set; }
    public Location Location { get; set; }
    public DateTime Timestamp { get; set; }
}

public class Asset
{
    public string AssetName { get; set; }
    public Hardware Hardware { get; set; }
}

public class Hardware
{
    public string SystemId { get; set; }
}

public class Location
{
    public string Lat { get; set; }
    public string Lng { get; set; }
}

相关问题