我正在从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": ""
}
}
}
答案 0 :(得分:1)
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);
}