How will i convert json string to Json Object [duplicate]

  • A+
Category:Languages

This question already has an answer here:

I have method which take json string, i want to convert json string to json object. I tried below method it show memory out of bound how to solve error. I tried this string throw ajax its working fine.But How would i convert to Data table from string in back end. Any Suggestion. here My Code.

      public ActionResult JosnString()         {        string str = "{/"delivery/": [{/"status/": 2, /"resp_msg/": /"5.4.1 [renard.allenll@transdev.com]: Recipient address rejected: Access denied [HE1EUR02FT054.eop-EUR02.prod.protection.outlook.com]/", /"mail_from/": /"vision@transdev.com/", /"time_started/": 1539808420, /"time_finished/": 1539808426, /"resp_code_description/": /"The requested command failed because the user's mailbox was unavailable (for example because it was not found, or because the command was rejected for policy reasons)./", /"sender_id/": 9007074, /"campaign_id/": /"/", /"rcpt_to/": /"renard.allenll@transdev.com/", /"tries/": 0, /"resp_code/": 550, /"tracking_id/": /"2939c3ea-59e8-4019-9f4b-4cd4214254b4/", /"resp_class/": 10, /"subject/": /"Las Vegas Notification - Passenger Fall- No Injury (Auto Email /u2013 Do Not Repl (#718701)/"}, {/"status/": 2, /"resp_msg/": /"5.4.1 [Theodrick.mccullom@transdev.com]: Recipient address rejected: Access denied [HE1EUR02FT042.eop-EUR02.prod.protection.outlook.com]/", /"mail_from/": /"vision@transdev.com/", /"time_started/": 1539809940, /"time_finished/": 1539809944, /"resp_code_description/": /"The requested command failed because the user's mailbox was unavailable (for example because it was not found, or because the command was rejected for policy reasons)./", /"sender_id/": 9007074, /"campaign_id/": /"/", /"rcpt_to/": /"theodrick.mccullom@transdev.com/", /"tries/": 0, /"resp_code/": 550, /"tracking_id/": /"2c75c0e1-cf9c-4c92-9d11-e6cebfc28300/", /"resp_class/": 10, /"subject/": /"Las Vegas Notification - Safety- Passenger Event (Auto Email /u2013 Do Not Reply) (#724957)/"}, {/"status/": 2, /"resp_msg/": /"5.4.1 [tricia.mumford@transdev.com]: Recipient address rejected: Access denied [VE1EUR02FT003.eop-EUR02.prod.protection.outlook.com]/", /"mail_from/": /"vision@transdev.com/", /"time_started/": 1539810919, /"time_finished/": 1539810926, /"resp_code_description/": /"The requested command failed because the user's mailbox was unavailable (for example because it was not found, or because the command was rejected for policy reasons)./", /"sender_id/": 9007074, /"campaign_id/": /"/", /"rcpt_to/": /"tricia.mumford@transdev.com/", /"tries/": 0, /"resp_code/": 550, /"tracking_id/": /"2f7a87e4-513e-4358-b1b6-bb8febdff35d/", /"resp_class/": 10, /"subject/": /"SV Alert Alert (#725159)/"}, {/"status/": 2, /"resp_msg/": /"5.4.1 [Theodrick.mccullom@transdev.com]: Recipient address rejected: Access denied [HE1EUR02FT033.eop-EUR02.prod.protection.outlook.com]/", /"mail_from/": /"vision@transdev.com/", /"time_started/": 1539815408, /"time_finished/": 1539815412, /"resp_code_description/": /"The requested command failed because the user's mailbox was unavailable (for example because it was not found, or because the command was rejected for policy reasons)./", /"sender_id/": 9007074, /"campaign_id/": /"/", /"rcpt_to/": /"theodrick.mccullom@transdev.com/", /"tries/": 0, /"resp_code/": 550, /"tracking_id/": /"e9bb47c0-22d5-4781-ac2a-9bef56232255/", /"resp_class/": 10, /"subject/": /"Las Vegas Notification - Safety- Accident with no injuries (Auto Email /u2013 Do (#733279)/"}, {/"status/": 2, /"resp_msg/": /"5.4.1 [francisco.sanchez@transdev.com]: Recipient address rejected: Access denied [HE1EUR02FT026.eop-EUR02.prod.protection.outlook.com]/", /"mail_from/": /"vision@transdev.com/", /"time_started/": 1539815465, /"time_finished/": 1539815469, /"resp_code_description/": /"The requested command failed because the user's mailbox was unavailable (for example because it was not found, or because the command was rejected for policy reasons)./", /"sender_id/": 9007074, /"campaign_id/": /"/", /"rcpt_to/": /"francisco.sanchez@transdev.com/", /"tries/": 0, /"resp_code/": 550, /"tracking_id/": /"c5baf56c-89da-4388-a692-da2b84862f0a/", /"resp_class/": 10, /"subject/": /"Malfunctioning GPS, please reboot this these device(s). (#733299)/"}, {/"status/": 2, /"resp_msg/": /"5.4.1 [carl.parr@transdev.com]: Recipient address rejected: Access denied [HE1EUR02FT019.eop-EUR02.prod.protection.outlook.com]/", /"mail_from/": /"vision@transdev.com/", /"time_started/": 1539816062, /"time_finished/": 1539816067, /"resp_code_description/": /"The requested command failed because the user's mailbox was unavailable (for example because it was not found, or because the command was rejected for policy reasons)./", /"sender_id/": 9007074, /"campaign_id/": /"/", /"rcpt_to/": /"carl.parr@transdev.com/", /"tries/": 0, /"resp_code/": 550, /"tracking_id/": /"e6299d04-5aac-43d3-8593-1a3c548e336f/", /"resp_class/": 10, /"subject/": /"SV Alert Alert (#733431)/"}, {/"status/": 2, /"resp_msg/": /"5.4.1 [Theodrick.mccullom@transdev.com]: Recipient address rejected: Access denied [HE1EUR02FT038.eop-EUR02.prod.protection.outlook.com]/", /"mail_from/": /"vision@transdev.com/", /"time_started/": 1539816315, /"time_finished/": 1539816318, /"resp_code_description/": /"The requested command failed because the user's mailbox was unavailable (for example because it was not found, or because the command was rejected for policy reasons)./", /"sender_id/": 9007074, /"campaign_id/": /"/", /"rcpt_to/": /"theodrick.mccullom@transdev.com/", /"tries/": 0, /"resp_code/": 550, /"tracking_id/": /"ea9caff3-364e-4a32-8365-98a897a6bbc5/", /"resp_class/": 10, /"subject/": /"Las Vegas Notification - Safety- Accident with no injuries (Auto Email /u2013 Do (#734371)/"}, {/"status/": 2, /"resp_msg/": /"5.4.1 [francisco.sanchez@transdev.com]: Recipient address rejected: Access denied [VE1EUR02FT035.eop-EUR02.prod.protection.outlook.com]/", /"mail_from/": /"vision@transdev.com/", /"time_started/": 1539820874, /"time_finished/": 1539820877, /"resp_code_description/": /"The requested command failed because the user's mailbox was unavailable (for example because it was not found, or because the command was rejected for policy reasons)./", /"sender_id/": 9007074, /"campaign_id/": /"/", /"rcpt_to/": /"francisco.sanchez@transdev.com/", /"tries/": 0, /"resp_code/": 550, /"tracking_id/": /"b81e47e5-5211-425a-a7be-5b75379bf3b1/", /"resp_class/": 10, /"subject/": /"Malfunctioning GPS, please reboot this these device(s). (#735305)/"}, {/"status/": 2, /"resp_msg/": /"5.4.1 [francisco.sanchez@transdev.com]: Recipient address rejected: Access denied [VE1EUR02FT053.eop-EUR02.prod.protection.outlook.com]/", /"mail_from/": /"vision@transdev.com/", /"time_started/": 1539826279, /"time_finished/": 1539826283, /"resp_code_description/": /"The requested command failed because the user's mailbox was unavailable (for example because it was not found, or because the command was rejected for policy reasons)./", /"sender_id/": 9007074, /"campaign_id/": /"/", /"rcpt_to/": /"francisco.sanchez@transdev.com/", /"tries/": 0, /"resp_code/": 550, /"tracking_id/": /"c2b7a520-a994-49a6-b34b-96113f5960d5/", /"resp_class/": 10, /"subject/": /"Malfunctioning GPS, please reboot this these device(s). (#735539)/"}, {/"status/": 2, /"resp_msg/": /"5.4.1 [Ramona.Trotter@transdev.com]: Recipient address rejected: Access denied [HE1EUR02FT023.eop-EUR02.prod.protection.outlook.com]/", /"mail_from/": /"vision@transdev.com/", /"time_started/": 1539829584, /"time_finished/": 1539829587, /"resp_code_description/": /"The requested command failed because the user's mailbox was unavailable (for example because it was not found, or because the command was rejected for policy reasons)./", /"sender_id/": 9007074, /"campaign_id/": /"/", /"rcpt_to/": /"ramona.trotter@transdev.com/", /"tries/": 0, /"resp_code/": 550, /"tracking_id/": /"264645cd-0792-445b-9fd4-1cccd0746cfb/", /"resp_class/": 10, /"subject/": /"Driver Dispatch Log Entries (Auto Email /u2013 Do Not Reply) (#736221)/"}], /"last_update_time/": 1540421479.703088}";                 JavaScriptSerializer serializer = new JavaScriptSerializer();                 DataSet dt = (DataSet)JsonConvert.DeserializeObject(str, (typeof(DataSet)));             return new EmptyResult(); } 

 


You are mixing thing here

JavaScriptSerializer belong to System.Web.Extensions

and JsonConvert to Json.net which JSON framework for .net

the easiest way is to use Json.net:

first, create a suitable class that match your json, you can use http://json2csharp.com/ to help you generate those classes

  public class Delivery         {             public int status { get; set; }             public string resp_msg { get; set; }             public string mail_from { get; set; }             public int time_started { get; set; }             public int time_finished { get; set; }             public string resp_code_description { get; set; }             public int sender_id { get; set; }             public string campaign_id { get; set; }             public string rcpt_to { get; set; }             public int tries { get; set; }             public int resp_code { get; set; }             public string tracking_id { get; set; }             public int resp_class { get; set; }             public string subject { get; set; }         }          public class DataRootObject         {             public List<Delivery> delivery { get; set; }             public double last_update_time { get; set; }         } 

And then simply use

var root = JsonConvert.DeserializeObject<DataRootObject>(str); 

and to access your object(s)

simply use your root object, like so:

var last_update_time = root.last_update_time; 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: