0

I have an issue to find the type of an object. I'm using MailChimp API and tried a couple of different scenarios. One of them is to send an incorrect email address (blablagmail.com, @ is missing).

I want to deserialize the json to be able to create a string with all the errors. Here the json with only the two properties that bug me:

"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."

I tried to cast errors as List<Dictionary<string, string>> but it doesn't work. The thing is the properties (email_address or error) can have a different name depending on the error.

Here the class I've created to deserialize the 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.

Any ideas?

Tyler Durden
  • 347
  • 1
  • 3
  • 15
  • Could you explain exactly what you mean by "I tried to cast error as `List>` but it doesn't work."? Ideally show the code you tried, and exactly what happened. Do you have to use the `JavaScriptSerializer`, or are you able to use Json.NET? – Jon Skeet Jun 08 '18 at 05:59
  • Also, what would you want to happen if you had multiple errors? If you could provide sample input and output for that, that would be useful. – Jon Skeet Jun 08 '18 at 05:59
  • Is that exactly how you see two properties or do they have curly braces at the beginning and at the end? – AD8 Jun 08 '18 at 06:01
  • 1
    Is this the complete json ? Because it's not a valid Json. – Drag and Drop Jun 08 '18 at 06:03
  • 1
    Quick tip: http://json2csharp.com ;) – Davy Jun 08 '18 at 06:04
  • I've updated my post. I can't use json.net or add any other references. – Tyler Durden Jun 08 '18 at 06:10
  • @Daisy That the thing, I want to create a function to take each property and value and create a unique string. In my example, I've two properties and two value so the string must be: str = "prop1:value1, prop2:val2, etc..." – Tyler Durden Jun 08 '18 at 06:13
  • @AD8 In my original post, this is the json but when I'm debugging, I see every item of errors like this : {[email_address, blablagmail.com]} , {[error, Please provide a valid email address.]} – Tyler Durden Jun 08 '18 at 06:15
  • Have you checked [this](https://stackoverflow.com/questions/1207731/how-can-i-deserialize-json-to-a-simple-dictionarystring-string-in-asp-net)? – Sangbok Lee Jun 08 '18 at 06:43
  • @TylerDurden: But you've got an *array* - so there could be multiple objects. Would you want that to be multiple strings? One string over multiple lines? Something else? – Jon Skeet Jun 08 '18 at 10:55

1 Answers1

1

I simulated your scenario in a Console application. Output of this sample is as you mentioned in your question. I used 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

email_address:blablagmail.com, error:Please provide a valid email address.

Gaurang Dave
  • 3,956
  • 2
  • 15
  • 34