Question

A typical set of message headers from a mailgun callback looks like this:

[["Received", "by luna.mailgun.net with SMTP mgrt 8765806286401; Fri, 14 Jun 2013 02:25:33 +0000"],
["Content-Type", ["multipart/mixed", {"boundary": "199d7350597e43c0af0e4c814b8a35af"}]],    
["Mime-Version", "1.0"], 
["Subject", "Test Message"],
["From", "Test Sender <sender@mydomain.com>"],
["To", "Test Receiver <receiver1@mydomain.com>"],
["Reply-To", "replyto@mydomain.com"], 
["Message-Id", "<20130614022533.18419.66130@mydomain.com>"], 
["X-Mailgun-Sid", "WyIzOTUwOCIsICJuZWlsLmRvYnNvbkBleGFsdGdyb3VwLmNvbS5hdSIsICI4ZjY3OCJd"], 
["Date", "Fri, 14 Jun 2013 02:25:33 +0000"], 
["Sender", "sender@mydomain.com"]]

I assumed this would deserialize to a key/value pair list but i'm unable to get this working. Obviously the Content-Type item is more complex.

Can anyone help parse this to a list or poco using ServiceStack or built-in NET deserializers?

Était-ce utile?

La solution

It's unfortunate the 3rd-party API returns the JSON in [["key","value"],...] arrays rather than {"key:"value", ...}. The latter would be much easier to parse into a typical class like this:

public class Foo
{
    public string Received {get; set;}
    [DataMember(Name = "Content-Type")]
    public string ContentType {get; set;}
    ... 
}

The way it returns is an anonymous list of values, containing more anonymous lists, where the 0 value is the key and the 1 value is the value.

I haven't tried it, but you may want to try deserializing to dynamic list of anonymous types, per this SO answer

List<JsonObject> jsonObject = JsonArrayObjects.Parse(json);
jsonObject.PrintDump(); // show data

Then for convenience, you could write a mapping routine to copy the jsonObject to a more readable class.

Update: i wrote a bit of code to test. Not that pretty, but it does work to access the parsed JSON as-is.

string json = @"[[""Received"", ""by luna.mailgun.net with SMTP mgrt 8765806286401; Fri, 14 Jun 2013 02:25:33 +0000""],
[""Content-Type"", [""multipart/mixed"", {""boundary"": ""199d7350597e43c0af0e4c814b8a35af""}]],    
[""Mime-Version"", ""1.0""], 
[""Subject"", ""Test Message""],
[""From"", ""Test Sender <sender@mydomain.com>""],
[""To"", ""Test Receiver <receiver1@mydomain.com>""],
[""Reply-To"", ""replyto@mydomain.com""], 
[""Message-Id"", ""<20130614022533.18419.66130@mydomain.com>""], 
[""X-Mailgun-Sid"", ""WyIzOTUwOCIsICJuZWlsLmRvYnNvbkBleGFsdGdyb3VwLmNvbS5hdSIsICI4ZjY3OCJd""], 
[""Date"", ""Fri, 14 Jun 2013 02:25:33 +0000""], 
[""Sender"", ""sender@mydomain.com""]]";

List<JsonObject> jsonObject = JsonArrayObjects.Parse(json);
// jsonObject.PrintDump(); // show data

string received = jsonObject[0].Values.First();
string contentType = jsonObject[1].Values.First();
List<JsonObject> contentTypeValues = JsonArrayObjects.Parse(jsonObject[1].Values.First());
string boundary = contentTypeValues[1].Values.First();
string mimeVersion = jsonObject[2].Values.First();

Console.WriteLine(string.Format("received: {0}", received));
Console.WriteLine(string.Format("contentType: {0}", contentType));
Console.WriteLine(string.Format("boundary: {0}", boundary));
Console.WriteLine(string.Format("mimeVersion: {0}", mimeVersion));
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top