如何将 JSON 字符串转换为 JSON 对象

3

我有一个方法,它接受json字符串,我想将json字符串转换为json对象。我尝试了下面的方法,但它会显示内存不足错误,该如何解决。 我尝试通过ajax传递字符串,它运行良好。但是我怎样才能在后端将其从字符串转换为数据表?有什么建议吗? 这是我的代码:

      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();
}

2
看起来你的字符串里有真实数据 - 把它编辑掉,你可能不想在这里发布。确切的错误信息是什么?不存在所谓的“内存越界异常”。 - Cee McSharpface
你想要一个 Dataset,为什么?为什么不使用更轻量级的 List<T>?接受的解决方案并没有做到预期的效果。仅仅创建一个动态类型是没有帮助的。 - Mrinal Kamboj
我尝试使用列表。 - MumbaiKandivali
4个回答

4
JObject json = JObject.Parse(str);

或者

using Newtonsoft.Json;


dynamic json  = JsonConvert.DeserializeObject(str);

这几乎不是一个解决方案,填充到JObject或Dynamic之后,如何创建一个数据集,正如OP所期望的那样。 - Mrinal Kamboj

3
你可以尝试这个。
string str = "{ 'context_name': { 'lower_bound': 'value', 'pper_bound': 'value', 'values': [ 'value1', 'valueN' ] } }";
    JavaScriptSerializer j = new JavaScriptSerializer();
    object a = j.Deserialize(str, typeof(object));

2

您在这里混淆了事情

JavaScriptSerializer 属于 System.Web.Extensions

JsonConvert 属于 Json.net,这是 .net 的 JSON 框架

最简单的方法是使用 Json.net:

首先,创建一个适合匹配您的 JSON 的类,您可以使用 http://json2csharp.com/ 来帮助您生成这些类

  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; }
        }

然后简单地使用:
var root = JsonConvert.DeserializeObject<DataRootObject>(str);

要访问您的对象,只需使用根对象,如下所示:

var last_update_time = root.last_update_time;

1
完成了。谢谢大家!!!! - MumbaiKandivali

2
string str = "{ 'context_name': { 'lower_bound': 'value', 'pper_bound': 'value', 'values': [ 'value1', 'valueN' ] } }";
JavaScriptSerializer j = new JavaScriptSerializer();
object a = j.Deserialize(str, typeof(object));

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接