c# - I have JSon response having objects with different names but all objects have same variables -
i connecting rest api c#. have json response few error objects different names objects have same variables: title, message, , display.
number of objects changes each request api (rest), names of objects in response different depending on requests. variables in each error same above.
the information need response message text, acceptable if list of error objects can read through messages errors.
here 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 '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 '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 add use newtonsoft.json,or can use regex this
string patten = @"""message""\s*:\s*""([^""]*)"""; foreach (match match in regex.matches(errortext, patten)) { console.writeline(match.groups[1].value); }
Comments
Post a Comment