2015-07-13 207 views
1

我正在连接来自c#的REST api。我有Json响应与几个不同名称的错误对象,但所有对象具有相同的变量: 标题, 消息和显示。我有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

'Newtonsoft.Json.JsonConvert。 DeserializeObject >(jsonStr)'当然,您必须定义一个类来包含字典 –

+0

是否要将json转换为c#对象? – gypsyCoder

+0

是@gypsyCoder – nicejani80

回答

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); 
} 
相关问题