[英]Json and deserialization
I have an issue to find the type of an object. 我有一个问题来找到对象的类型。 I'm using MailChimp API and tried a couple of different scenarios.
我正在使用MailChimp API,并尝试了几种不同的情况。 One of them is to send an incorrect email address (blablagmail.com, @ is missing).
其中之一是发送错误的电子邮件地址(blablagmail.com,@丢失)。
I want to deserialize the json to be able to create a string with all the errors. 我想反序列化json,以便能够创建一个包含所有错误的字符串。 Here the json with only the two properties that bug me:
在这里,只有两个属性的json会困扰我:
"error_count":1,
"errors":[
{
"email_address":"blablagmail.com",
"error":"Please provide a valid email address."
}
],
Basically, what I try to do is to is to create an unique string with all the erros for my logger. 基本上,我想做的是为记录器创建一个包含所有错误的唯一字符串。 Something like:
就像是:
errors = "email_address:blablagmail.com, error:Please provide a valid email address." 错误=“电子邮件地址:blablagmail.com,错误:请提供有效的电子邮件地址。”
I tried to cast errors
as List<Dictionary<string, string>>
but it doesn't work. 我试图将
errors
为List<Dictionary<string, string>>
但是它不起作用。 The thing is the properties ( email_address
or error
) can have a different name depending on the error. 关键是属性(
email_address
或error
)可以根据error
而具有不同的名称。
Here the class I've created to deserialize the json 这是我创建的用于反序列化json的类
public class MailChimpResponse
{
public object[] new_members { get; set; }
public object[] updated_members { get; set; }
public object[] errors { get; set; }
public int total_created { get; set; }
public int total_updated { get; set; }
public int error_count { get; set; }
}
MailChimpResponse obj = new JavaScriptSerializer().Deserialize<MailChimpResponse>(response);
Now if error_count > 0 I want to log the errors so I would like to create a genreic function to convert this array of object ( object[] errors
) to a string. 现在,如果error_count> 0,我想记录错误,因此我想创建一个体裁函数,将对象数组(
object[] errors
)转换为字符串。
Any ideas? 有任何想法吗?
I simulated your scenario in a Console application. 我在控制台应用程序中模拟了您的方案。 Output of this sample is as you mentioned in your question.
如您在问题中提到的,此样本的输出。 I used
JavaScriptSerializer
. 我使用了
JavaScriptSerializer
。
class Program
{
static void Main(string[] args)
{
string json = "{\"error_count\":1,\"errors\":[{\"email_address\":\"blablagmail.com\",\"error\":\"Please provide a valid email address.\"}]}";
MailChimpResponse obj = new JavaScriptSerializer().Deserialize<MailChimpResponse>(json);
if (obj != null && obj.error_count > 0 && obj.errors!=null)
{
string errorString = "";
foreach (var error in obj.errors)
{
var casting = (Dictionary<string,object>)error;
errorString = string.Join(",", casting.Select(x => x.Key + ":" + x.Value).ToArray());
}
Console.WriteLine(errorString);
}
Console.ReadLine();
}
}
public class MailChimpResponse
{
public object[] new_members { get; set; }
public object[] updated_members { get; set; }
public object[] errors { get; set; }
public int total_created { get; set; }
public int total_updated { get; set; }
public int error_count { get; set; }
}
OUTPUT OUTPUT
email_address:blablagmail.com, error:Please provide a valid email address.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.