gpt4 book ai didi

c# - JsonConvert DeserializeObject 区分大小写

转载 作者:太空狗 更新时间:2023-10-30 01:31:59 31 4
gpt4 key购买 nike

<分区>

我正在尝试将字符串内容反序列化为对象,但我希望内容区分大小写。只有当字符串具有小写属性时,代码才应该成功,而如果它具有大写属性,则代码应该失败。以下是类(class):

internal class ResponseList
{
[DataMember]
[JsonProperty]
internal List<Response> Value { get; set; }
}

internal class Response
{
[DataMember]
[JsonProperty]
internal string Id { get; set; }

[DataMember]
[JsonProperty]
internal string Location { get; set; }

[DataMember]
[JsonProperty]
internal PlanClass Plan { get; set; }
}

internal class PlanClass
{
[DataMember]
[JsonProperty]
internal string Name { get; set; }

[DataMember]
[JsonProperty]
internal string Product { get; set; }

[DataMember]
[JsonProperty]
internal string Publisher { get; set; }
}

以下是我的代码。但这不区分大小写。大写和小写都成功:

string content = File.ReadAllText(contentFilePath);
JsonSerializerSettings jsonSerializerSettings1 = new JsonSerializerSettings()
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
};
ResponseList response = (ResponseList)JsonConvert.DeserializeObject(contentResourceOutput, typeof(ResponseList), Constants.JsonSerializerSettings);

代码应该只有在内容是:

{
"value": [
{
"id": "id1",
"location": "location1",
"plan": {
"name": "free",
"product": "product1",
"publisher": "publisher1"
}
}
]
}

即使其中一个键是大写也会失败。例如。

{
"value": [
{
"Id": "id1",
"Location": "location1",
"plan": {
"Name": "free",
"product": "product1",
"publisher": "publisher1"
}
}
]
}

请注意,只有键/属性名称应该是小写的。值可以是大写。有没有办法让 JsonConvert.Deserializeobject 区分大小写?

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