gpt4 book ai didi

c# - 我有 JSON 响应具有不同名称的对象,但所有对象都具有相同的变量

转载 作者:太空宇宙 更新时间:2023-11-03 13:04:00 28 4
gpt4 key购买 nike

我正在从 C# 连接一个 REST api。我有 Json 响应,其中包含几个具有不同名称的错误对象,但所有对象都具有相同的变量:标题,消息,并显示。

对象的数量随每个 API (REST) 请求而变化,响应对象的名称因请求而异。但是每个错误中的变量与上面相同。

我需要从该响应中获得的信息只是消息文本,但如果我获得错误对象列表以便我可以通读错误消息,这将是可以接受的。

这是 JSON 响应:

  {
"errors": {
"missingparameter_general_paymenttype": {
"title": "",
"message": "You must enter 'general_paymenttype'.",
"display": ""
},
"missingparameter_contact_title": {
"title": "",
"message": "You must enter 'contact_title'.",
"display": ""
},
"missingparameter_contact_firstname": {
"title": "",
"message": "You must enter 'contact_firstname'.",
"display": ""
},
"missingparameter_contact_lastname": {
"title": "",
"message": "You must enter 'contact_lastname'.",
"display": ""
},
"missingparameter_contact_email": {
"title": "",
"message": "You must enter 'contact_email'.",
"display": ""
},
"missingparameter_contact_telephone": {
"title": "",
"message": "You must enter 'contact_telephone'.",
"display": ""
},
"invalidparameter_pricing_currency": {
"title": "",
"message": "Invalid value for 'pricing_currency'.",
"display": ""
},
"missingparameter_pricing_saleprice": {
"title": "",
"message": "You must enter 'pricing_saleprice'.",
"display": ""
},
"missingparameter_transfers": {
"title": "",
"message": "You must enter 'transfers'.",
"display": ""
}
}
}

最佳答案

class Errors
{
public Dictionary<string, Error> errors { get; set; }
public class Error
{
public string title { get; set; }
public string message { get; set; }
public string display { get; set; }
}
}

static void Main(string[] args)
{
string errorText = @"{
""errors"": {
""missingparameter_general_paymenttype"": {
""title"": """",
""message"": ""You must enter 'general_paymenttype'."",
""display"": """"
},
""missingparameter_contact_title"": {
""title"": """",
""message"": ""You must enter 'contact_title'."",
""display"": """"
},
""missingparameter_contact_firstname"": {
""title"": """",
""message"": ""You must enter 'contact_firstname'."",
""display"": """"
},
""missingparameter_contact_lastname"": {
""title"": """",
""message"": ""You must enter 'contact_lastname'."",
""display"": """"
},
""missingparameter_contact_email"": {
""title"": """",
""message"": ""You must enter 'contact_email'."",
""display"": """"
},
""missingparameter_contact_telephone"": {
""title"": """",
""message"": ""You must enter 'contact_telephone'."",
""display"": """"
},
""invalidparameter_pricing_currency"": {
""title"": """",
""message"": ""Invalid value for 'pricing_currency'."",
""display"": """"
},
""missingparameter_pricing_saleprice"": {
""title"": """",
""message"": ""You must enter 'pricing_saleprice'."",
""display"": """"
},
""missingparameter_transfers"": {
""title"": """",
""message"": ""You must enter 'transfers'."",
""display"": """"
}
}}";
var error = JsonConvert.DeserializeObject<Errors>(errorText);
foreach (var kv in error.errors)
{
Console.WriteLine(kv.Value.message);
}
}

你需要添加使用Newtonsoft.Json,或者你也可以像这样使用Regex

string patten = @"""message""\s*:\s*""([^""]*)""";
foreach (Match match in Regex.Matches(errorText, patten))
{
Console.WriteLine(match.Groups[1].Value);
}

关于c# - 我有 JSON 响应具有不同名称的对象,但所有对象都具有相同的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31385493/

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