0

I am trying to pass along a JSON string for an API I'm writing. Here's what the JSON post looks like:

{"AssociatedApplication":"Postman", "Key":"a274e012c52d4121bda7d1e7a7218cf5", "Subject":"Testing API", "Body":"Testing email API. Muffins.",
"EmailAddresses":[
    {"From":"[email protected]"},
    {"To":"[email protected]"},
    {"To":"[email protected]"},
    {"Cc":"[email protected]"},
    {"Bcc":"[email protected]"}
]
}

Using JSON2Csharp, I have my classes set up as

[NotMapped]
class EmailTransmission
{
    public string AssociatedApplication;
    public string Key;
    public string Subject;
    public string Body;
    public List<EmailAddress> EmailAddresses;
}

[NotMapped]
public class EmailAddress
{
    public string From { get; set; }
    public string To { get; set; }
    public string Cc { get; set; }
    public string Bcc { get; set; }
}

When I use JsonConvert.DeserializeObject<List<EmailAddress>>(email["EmailAddresses"]) it doesn't work and I've tried List<List<string>> List and so forth. What is the type I should use without getting a runtime type binder exception?

2 Answers 2

3

If that's actually your code, you should be using properties in EmailTransmission, not fields.

Second, your class EmailTransmission needs to be public.

Finally, you should just deserialize the entire object, then pull out what you need afterwards.

JsonConvert.DeserializeObject<EmailTransmission>(email);

Once you've deserialized the entire json string, you can pull off the email addresses.

Sign up to request clarification or add additional context in comments.

1 Comment

@Dreamcasting happy to help!
0

Please change your EmailTransmission class as belows

[NotMapped]
class EmailTransmission
{
    public string AssociatedApplication { get; set; }
    public string Key { get; set; }
    public string Subject { get; set; }
    public string Body { get; set; }
    public List<EmailAddress> EmailAddresses { get; set; }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.