gpt4 book ai didi

c# - 将 JSON 字符串反序列化为 C# 类

转载 作者:行者123 更新时间:2023-11-30 20:31:39 32 4
gpt4 key购买 nike

好的,我会尽量简短具体。我从一个公共(public) API 获取一个 JSON 字符串,如下所示:(它的简短版本,只取了 2 个样本)

[{
"$type": "Tfl.Api.Presentation.Entities.Line, Tfl.Api.Presentation.Entities",
"id": "bakerloo",
"name": "Bakerloo",
"modeName": "tube",
"disruptions": [],
"created": "2017-03-16T15:56:01.01Z",
"modified": "2017-03-16T15:56:01.01Z",
"lineStatuses": [
{
"$type": "Tfl.Api.Presentation.Entities.LineStatus, Tfl.Api.Presentation.Entities",
"id": 0,
"statusSeverity": 10,
"statusSeverityDescription": "Good Service",
"created": "0001-01-01T00:00:00",
"validityPeriods": []
}
],
"routeSections": [],
"serviceTypes": [
{
"$type": "Tfl.Api.Presentation.Entities.LineServiceTypeInfo, Tfl.Api.Presentation.Entities",
"name": "Regular",
"uri": "/Line/Route?ids=Bakerloo&serviceTypes=Regular"
}
],
"crowding": {
"$type": "Tfl.Api.Presentation.Entities.Crowding, Tfl.Api.Presentation.Entities"
}
},
{
"$type": "Tfl.Api.Presentation.Entities.Line, Tfl.Api.Presentation.Entities",
"id": "central",
"name": "Central",
"modeName": "tube",
"disruptions": [],
"created": "2017-03-16T15:56:01.01Z",
"modified": "2017-03-16T15:56:01.01Z",
"lineStatuses": [
{
"$type": "Tfl.Api.Presentation.Entities.LineStatus, Tfl.Api.Presentation.Entities",
"id": 0,
"statusSeverity": 10,
"statusSeverityDescription": "Good Service",
"created": "0001-01-01T00:00:00",
"validityPeriods": []
}
],
"routeSections": [],
"serviceTypes": [
{
"$type": "Tfl.Api.Presentation.Entities.LineServiceTypeInfo, Tfl.Api.Presentation.Entities",
"name": "Regular",
"uri": "/Line/Route?ids=Central&serviceTypes=Regular"
},
{
"$type": "Tfl.Api.Presentation.Entities.LineServiceTypeInfo, Tfl.Api.Presentation.Entities",
"name": "Night",
"uri": "/Line/Route?ids=Central&serviceTypes=Night"
}
],
"crowding": {
"$type": "Tfl.Api.Presentation.Entities.Crowding, Tfl.Api.Presentation.Entities"
}
}]

到目前为止一切顺利,下面我将粘贴我正在使用的相关代码并尝试将此 JSON 字符串反序列化到我从 json2csharp 获得的 C# 类中免费在线服务。我试图实现这一目标的相关代码是:

public async static Task<tubeStatusRootObject> GetTubeStatus(string url)
{
var http = new HttpClient();
var response = await http.GetAsync(url);
var result = await response.Content.ReadAsStringAsync(); //This is working
var deserializer = new DataContractJsonSerializer(typeof(tubeStatusRootObject));
var ms = new MemoryStream(Encoding.UTF8.GetBytes(result));
var data = (tubeStatusRootObject)deserializer.ReadObject(ms);
return data; //all "data" properties are null
}

正如你可以阅读上面的评论,我在返回之前得到的所有内部数据都是空的。

由 json2csharp 生成的类如下所示:

[DataContract]
public class Disruption
{
[DataMember]
public string type { get; set; }

[DataMember]
public string category { get; set; }

[DataMember]
public string categoryDescription { get; set; }

[DataMember]
public string description { get; set; }

[DataMember]
public string additionalInfo { get; set; }

[DataMember]
public string created { get; set; }

[DataMember]
public List<object> affectedRoutes { get; set; }

[DataMember]
public List<object> affectedStops { get; set; }

[DataMember]
public string closureText { get; set; }

[DataMember]
public bool? isWholeLine { get; set; }
}

[DataContract]
public class LineStatus
{
[DataMember]
public string type { get; set; }

[DataMember]
public int id { get; set; }

[DataMember]
public int statusSeverity { get; set; }

[DataMember]
public string statusSeverityDescription { get; set; }

[DataMember]
public string created { get; set; }

[DataMember]
public List<object> validityPeriods { get; set; }

[DataMember]
public string lineId { get; set; }

[DataMember]
public string reason { get; set; }

[DataMember]
public Disruption disruption { get; set; }
}

[DataContract]
public class ServiceType
{
[DataMember]
public string type { get; set; }

[DataMember]
public string name { get; set; }

[DataMember]
public string uri { get; set; }
}

[DataContract]
public class Crowding
{
[DataMember]
public string type { get; set; }
}

[DataContract]
public class tubeStatusRootObject
{
[DataMember]
public string type { get; set; }

[DataMember]
public string id { get; set; }

[DataMember]
public string name { get; set; }

[DataMember]
public string modeName { get; set; }

[DataMember]
public List<object> disruptions { get; set; }

[DataMember]
public string created { get; set; }

[DataMember]
public string modified { get; set; }

[DataMember]
public List<LineStatus> lineStatuses { get; set; }

[DataMember]
public List<object> routeSections { get; set; }

[DataMember]
public List<ServiceType> serviceTypes { get; set; }

[DataMember]
public Crowding crowding { get; set; }
}

显然,我只是在其中添加了 [DataContract][DataMember]。任何知道我做错了什么并且可以帮助我的人

我遵循了 Channel9 中的示例

请不要标记重复,因为我发现了很多类似的问题,有些问题使用 newtonsoft json 但我无法将那里的解决方案实现到我的示例中

最佳答案

提供的示例数据描述了一个数组 (tubeStatusRootObject[]) 但是当您尝试反序列化时,您将其转换为单个实例,这是一个无效的转换。这就是为什么 datanull 的原因。

如果有可用的工具来简单地解决问题,也无需重新发明轮子。

static http = new HttpClient(); //reuse httpclient instead of creating a new one each time.
public async static Task<tubeStatusRootObject[]> GetTubeStatus(string url) {
var response = await http.GetAsync(url);
var json = await response.Content.ReadAsStringAsync(); //This is working
var data = Newtonsoft.Json.JsonConvert.DeserializeObject<tubeStatusRootObject[]>(json);
return data;
}

关于c# - 将 JSON 字符串反序列化为 C# 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42885806/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com