3

I am using Sendgrid API to send and retrieve statistics of mail sent. I want to store the response of API in database.

protected void btnBounces_Click(object sender, EventArgs e)
{
    try
    {
        string url = "https://api.sendgrid.com/api/bounces.get.json";
        GetResult(url);
    }
    catch (Exception ex)
    {
        lblError.Text = ex.Message.ToString();
    }
}
 public void GetResult(string url)
{
    string parameters = "api_user=xxxx&api_key=xxxx&date=1&start_date="+txtStartDate.Text+"&end_date="+txtEndDate.Text;
    // Start the request
    HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
    req.Method = "POST";
    req.ContentType = "application/x-www-form-urlencoded";
    StreamWriter streamWriter = new StreamWriter(req.GetRequestStream());
    streamWriter.Write(parameters);
    streamWriter.Flush();
    streamWriter.Close();
    // Get the response
    HttpWebResponse res = (HttpWebResponse)req.GetResponse();
    StreamReader streamReader = new StreamReader(res.GetResponseStream());
    string result = streamReader.ReadToEnd();
}

The response I will get will be like:

[
  {
    "status": "4.0.0",
    "created": "2011-09-16 22:02:19",
    "reason": "Unable to resolve MX host sendgrid.ne",
    "email": "esting@sendgrid.ne"
  },
  {
    "status": "4.0.0",
    "created": "2011-09-19 17:47:15",
    "reason": "Connection timed out",
    "email": "rawest@gmail.co"
  }
]

How can i extract value of each of the four fields and store them in table containing four fields?

antpaw
  • 15,444
  • 11
  • 59
  • 88
Ami
  • 397
  • 2
  • 4
  • 19
  • 1
    Thanks Nagaraj S for editing my question but I want answer. Help me. – Ami Jan 28 '14 at 11:58
  • There are many ways to do this. What kind of database are you using? – bwest Jan 29 '14 at 16:44
  • So your question right now is extremely broad. It's basically "how do I interact with a mysql database using ASP.NET" and there a lot of ways to do that. What are you using to interact with the database from code right now? If you don't have any way to do that, you should do some research and choose a method, and then I can help you with the sendgrid-specific stuff. – bwest Feb 05 '14 at 16:37
  • I will be using SqlClient. This is not important. I just want to kno that how can I extract each value from reply. – Ami Feb 06 '14 at 06:16
  • 1
    [This might help understand](http://stackoverflow.com/questions/17754414/handling-json-data-using-jquery-sent-by-using-newtonsoft-json) – Bhavik Feb 23 '14 at 18:06

3 Answers3

4

If you do not want to create a Movie class, you can use System.Web.Script.Serialization to parse and get a dynamic object.

JavaScriptSerializer js = new JavaScriptSerializer();
dynamic movie = js.Deserialize<dynamic>(json);
Rafa Paez
  • 4,820
  • 18
  • 35
3

To read a JSON string in .NET, I recommend using Newtonsoft Json.NET.

Here is a simple example of deserializing a string into an object and reading a property:

string json = @"{
  'Name': 'Bad Boys',
  'ReleaseDate': '1995-4-7T00:00:00',
  'Genres': [
    'Action',
    'Comedy'
  ]
}";

Movie m = JsonConvert.DeserializeObject<Movie>(json);

string name = m.Name;
// Bad Boys

Note that you will have to define the object that you want to deserialize the values into ahead of time; you can see that in this example there is a Type Movie that already exists, and the JsonConverter matches the properties in the JSON string to the properties of that type.

bwest
  • 9,182
  • 3
  • 28
  • 58
1

I got the answer

     public class BouncesAndBlocks
{
    public string status { get; set; }
    public string created { get; set; }
    public string email { get; set; }
    public string reason { get; set; }
}
     protected void btnBounces_Click(object sender, EventArgs e)
{
    string url = "https://api.sendgrid.com/api/bounces.get.json";
    string JS = GetResult(url);
    List<BouncesAndBlocks> res = (List<BouncesAndBlocks>)JsonConvert.DeserializeObject(JS, typeof(List<BouncesAndBlocks>));
    foreach (var item in res)
    {
        //Store in database
        lbl.Text = "Date:" + item.created.ToString() + "    Status:" + item.status.ToString() + "     Email:" + item.email.ToString() + "       Message:" + item.reason.ToString() + "";

    }
}
Ami
  • 397
  • 2
  • 4
  • 19