Reputation: 1705
I am connecting a REST
api from c#. I have Json
response with few error objects with different names but all the objects have same variables:
title,
message, and display.
Number of objects changes with each request to API (REST), the names of objects in response are different depending on requests. But the variables in each error are same as above.
The information I need from this response is only message text, but it will be acceptable if I get list of error objects so I can read through the messages from errors.
Here is the JSon response:
{
"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": ""
}
}
}
Upvotes: 2
Views: 544
Reputation: 1101
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);
}
}
You need to add use Newtonsoft.Json,Or you can also use Regex like this
string patten = @"""message""\s*:\s*""([^""]*)""";
foreach (Match match in Regex.Matches(errorText, patten))
{
Console.WriteLine(match.Groups[1].Value);
}
Upvotes: 1