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

18 浏览
0 Comments

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

我有一个方法可以接受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();
}

0
0 Comments

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

问题的出现原因:在开发过程中,我们经常会遇到需要将JSON字符串转换为JSON对象的情况。这可能是因为我们从服务器获取到的数据是以JSON字符串的形式返回的,而我们需要将其转换为JSON对象来方便地操作和访问数据。

解决方法:下面是一种解决方法,可以使用JavaScriptSerializer类的Deserialize方法将JSON字符串转换为JSON对象。

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

上述代码中,我们首先定义了一个JSON字符串 `str`,然后使用JavaScriptSerializer类的Deserialize方法将其转换为JSON对象。在Deserialize方法中,我们传递了两个参数,第一个参数是要转换的JSON字符串,第二个参数是要转换成的对象类型(在本例中是object类型)。

通过上述代码,我们可以将JSON字符串转换为JSON对象,然后就可以方便地操作和访问其中的数据了。

通过使用JavaScriptSerializer类的Deserialize方法,我们可以将JSON字符串转换为JSON对象,从而方便地操作和访问其中的数据。这是一种常用的解决方法,可以帮助我们在开发过程中处理JSON数据。

0
0 Comments

问题的出现原因:用户混淆了使用不同的JSON库(System.Web.Extensions和Json.net)来进行JSON字符串到JSON对象的转换。

解决方法:使用Json.net库来进行转换。首先,根据JSON字符串创建一个适合的类,可以使用json2csharp网站来帮助生成这些类。然后,使用JsonConvert.DeserializeObject方法将JSON字符串转换为JSON对象,并使用root对象来访问需要的属性。最后,通过这种方法完成了JSON字符串到JSON对象的转换。

0
0 Comments

问题的原因是在将JSON字符串转换为JSON对象时,使用了JObject.Parse或JsonConvert.DeserializeObject方法,但这些方法只能将JSON字符串转换为JObject或dynamic类型的对象,而无法直接创建Dataset对象,这与问题提出者的预期不符。

解决方法是使用JsonConvert.DeserializeObject方法将JSON字符串转换为匿名类型的对象,然后使用Newtonsoft.Json.Linq.JObject.FromObject方法将匿名类型的对象转换为JObject对象,最后使用JObject.ToObject方法将JObject对象转换为Dataset对象。

下面是解决问题的代码示例:

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Data;
string jsonStr = @"{
  'Table1': [
    {
      'Column1': 'Value1',
      'Column2': 'Value2'
    },
    {
      'Column1': 'Value3',
      'Column2': 'Value4'
    }
  ]
}";
dynamic jsonObj = JsonConvert.DeserializeObject(jsonStr);
JObject jObject = JObject.FromObject(jsonObj);
Dataset dataset = jObject.ToObject();

以上代码首先定义了一个包含JSON字符串的变量jsonStr。然后使用JsonConvert.DeserializeObject方法将JSON字符串转换为dynamic类型的对象jsonObj。接着使用JObject.FromObject方法将jsonObj对象转换为JObject对象jObject。最后使用jObject.ToObject方法将jObject对象转换为Dataset对象dataset。

通过以上解决方法,可以实现将JSON字符串转换为Dataset对象的需求。

0