locked
Deserialize json data and save in DataBase RRS feed

  • Question

  • User229488726 posted

    I am new in json.
    In my project i need to deserialize json data come from amazon service url .

    this is my json format

    {"complaint@simulator.amazonses.com":{"time":"2018-01-02T20:45:46.650Z","type":"Complaint","bounceType":"null","bounceSubType":"null"},
    "struax@afb.net":{"time":"2018-01-02T20:53:03.000Z","type":"Bounce","bounceType":"Permanent","bounceSubType":"Suppressed"},
    "bounce-test@service.socketlabs.com":{"time":"2018-01-02T21:06:40.097Z","type":"Bounce","bounceType":"Permanent","bounceSubType":"Suppressed"},
    "bounce@simulator.amazonses.com":{"time":"2018-01-02T21:08:02.000Z","type":"Bounce","bounceType":"Permanent","bounceSubType":"General"},
    "jstrechay@afb.net":{"time":"2018-01-05T06:31:39.000Z","type":"Bounce","bounceType":"Permanent","bounceSubType":"General"},
    "leematt45@hotmail.com":{"time":"2018-01-05T06:49:13.000Z","type":"Bounce","bounceType":"Permanent","bounceSubType":"Suppressed"},
    "afbweb@afb.net":{"time":"2018-01-07T12:50:38.000Z","type":"Bounce","bounceType":"Transient","bounceSubType":"General"},
    "rushjenacath@gmail.com":{"time":"2018-01-05T06:32:42.000Z","type":"Bounce","bounceType":"Permanent","bounceSubType":"Suppressed"},
    "Kerryhoskins0925@gmaiil.com":{"time":"2018-01-02T09:18:20.000Z","type":"Bounce","bounceType":"Transient","bounceSubType":"General"}}

    i need to ave this record in db and my db table column's are Email ,type, time,bounceType, bounceSubType.
    i am not understand how to do this
    thanks in advance

    this is i tried

    using (WebClient wc = new WebClient())
    {
    var json = wc.DownloadString("https://s3.amazonaws.com/afb-ses/bounce.json");
    string jsonData = JsonConvert.SerializeObject(json);
    var _userin = JsonConvert.DeserializeObject(jsonData);

    }

    after Deserialize it look like below . how save those data in DB

    image

    Tuesday, January 9, 2018 2:33 PM

All replies

  • User-2054057000 posted

    The last line of your code is:

    var _userin = JsonConvert.DeserializeObject(jsonData);

    Change it to:

     ResponseAmazon _userin = JsonConvert.DeserializeObject<ResponseAmazon>(jsonData);

    ResponseAmazon will be a class which you will make. It will contain the json data. You can easily make this class structure from the amazon json. For this you can use - http://json2csharp.com

    So ones the json data is added to class (ResponseAmazon), you can then easily add it to the database right ?

    You can see this tutorial which does the same thing from json to class conversion. 

    I hope I am pointing you to the right direction.

    Wednesday, January 10, 2018 5:32 AM
  • User-474980206 posted

    your json maps to a Dictionary<string,Bounce> where Bounce is a class that defines a properties, time, type, etc.

    Wednesday, January 10, 2018 11:14 PM
  • User-832373396 posted

    <g class="gr_ gr_112 gr-alert gr_gramm gr_inline_cards gr_run_anim Punctuation only-ins replaceWithoutSep" id="112" data-gr-id="112">Hi</g> <g class="gr_ gr_5 gr-alert gr_spell gr_inline_cards gr_disable_anim_appear ContextualSpelling" id="5" data-gr-id="5">brajalalbrp</g>,

    i need to <g class="gr_ gr_118 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del" id="118" data-gr-id="118">ave</g> this record in <g class="gr_ gr_119 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="119" data-gr-id="119">db</g> and my <g class="gr_ gr_120 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="120" data-gr-id="120">db</g> table column's are Email ,type, time,<g class="gr_ gr_121 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="121" data-gr-id="121">bounceType</g>, bounceSubType.

    Sir, here is the working example to use JObject:

    First of all, add a class:

    public class Myclass
            {
                public DateTime time { get; set; }
                public string type { get; set; }
                public string bounceType { get; set; }
                public string bounceSubType { get; set; }
                public string email { get; set; }
            }

    2 full code:

    string path = Path.Combine(@"C:\Content", "json.json");
                JObject jObject = JObject.Parse(System.IO.File.ReadAllText(path) );           
                List<Myclass> list = new List<Myclass>();
                foreach (var u in jObject)
                {                
                    Myclass cc = u.Value.ToObject<Myclass>();
    // u.Value is {{ "time": "2018-01-02T20:45:46.65Z", "type": "Complaint", "bounceType": "null", "bounceSubType": "null"}}
    cc.email = u.Key;
    //u.key is complaint@simulator.amazonses.com list.Add(cc); //list, it is the result }
    Next step:
    save into database with List<Myclass> collection

    Then it fills the list collection :

    Test <g class="gr_ gr_677 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling" id="677" data-gr-id="677">json</g>.<g class="gr_ gr_700 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="700" data-gr-id="700">json</g> :

    {
      "complaint@simulator.amazonses.com": {
        "time": "2018-01-02T20:45:46.650Z",
        "type": "Complaint",
        "bounceType": "null",
        "bounceSubType": "null"
      },
      "struax@afb.net": {
        "time": "2018-01-02T20:53:03.000Z",
        "type": "Bounce",
        "bounceType": "Permanent",
        "bounceSubType": "Suppressed"
      }
    }

    Reference:

    https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_Linq_JObject.htm 

    With regards, Angelina Jolie

    Thursday, January 11, 2018 8:36 AM