-2

I mostly work on the PHP , recently have switched to ASP.NET,

When parse the JSON, I can simply use -> to get the field, e.g.

foreach(json_decode($_POST['mandrill_events']) as $event) {
    $event = $event->event;
    $email_type = $event->msg->metadata->email_type;
}

However, in ASP.NET , there is no action, this is my attempt code

var post_data =  Request.Form["mandrill_events"];

JavaScriptSerializer ser = new JavaScriptSerializer();
var post_data_json = ser.Deserialize<Dictionary<string, string>>(post_data);

foreach (var event_obj in post_data_json) {
   //how to parse the event_obj?
}

Thanks a lot for helping.

1

2 Answers 2

2

use Newtonsoft Json.NET

JsonConvert.DeserializeObject<DataModel>(json);
Sign up to request clarification or add additional context in comments.

Comments

1

Unless you want to write a C# class that represents the JSON you are POSTing (the safest solution), you can use the dynamic type to create an object which will look like your JSON. You can then do something like this answer to access the properties.

This solution doesn't give you type safety and the DLR will resolve the properties of the dynamic object at runtime.

As other answers have mentioned, your life will be made much easier by using Newtonsoft JSON which will allow you to write:

dynamic events = JsonConvert.DeserializeObject<dynamic>(post_data);

foreach(dynamic evt in events)
{
    string emailType = evt.msg.metadata.email_type;
}

2 Comments

how to handle the case e.g. $event->msg->metadata->email_type? thanks
@user782104 sorry, I didn't notice that, I will update my answer.

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.