我有 JSon 响应具有不同名称的对象,但所有对象都具有相同的变量 [英] I have JSon response having objects with different names but all objects have same variables

查看:48
本文介绍了我有 JSon 响应具有不同名称的对象,但所有对象都具有相同的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从 c# 连接一个 REST api.我有 Json 响应,其中包含几个名称不同的错误对象,但所有对象都具有相同的变量:标题,消息,并显示.

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.

对象数量随着对 API (REST) 的每个请求而变化,响应的对象名称因请求而异.但是每个错误中的变量都和上面一样.

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.

这是 JSon 的回复:

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": ""
        }
    }
}

推荐答案

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);
    }
}

你需要添加use Newtonsoft.Json,或者你也可以像这样使用Regex

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);
}

这篇关于我有 JSon 响应具有不同名称的对象,但所有对象都具有相同的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆