我有不同的名称JSON响应为对象,但所有对象都具有相同的变量(I have JSon respons

2019-10-23 19:55发布

我连接一个REST从C#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": ""
        }
    }
}

Answer 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,或者你也可以使用正则表达式这样

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


文章来源: I have JSon response having objects with different names but all objects have same variables
标签: c# json rest