Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我有一个JSon响应,它有不同名称的对象,但所有对象都有相同的变量_C#_Json_Rest - Fatal编程技术网

C# 我有一个JSon响应,它有不同名称的对象,但所有对象都有相同的变量

C# 我有一个JSon响应,它有不同名称的对象,但所有对象都有相同的变量,c#,json,rest,C#,Json,Rest,我正在从c#连接一个RESTapi。我有Json响应,有几个不同名称的错误对象,但所有对象都有相同的变量: 标题 消息和显示 对象的数量随着每个对API的请求(REST)而变化,响应对象的名称根据请求的不同而不同。但每个错误中的变量与上述相同 我从这个响应中需要的信息只是消息文本,但是如果我得到错误对象的列表,这样我就可以从错误中读取消息,这是可以接受的 以下是JSon响应: { "errors": { "missingparameter_general_paymen

我正在从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": ""
        }
    }
}

Newtonsoft.Json.JsonConvert.DeserializeObject(jsonStr)
当然,您必须定义一个类来包含字典。您想将Json转换为c#对象吗?
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);
    }
}
string patten = @"""message""\s*:\s*""([^""]*)""";
foreach (Match match in Regex.Matches(errorText, patten))
{
    Console.WriteLine(match.Groups[1].Value);
}