下面我用一个实例来和大家分享一下我的经验,asp.net MVC 框架中控制器里使用Newtonsoft.Json对前端传过来的字符串进行解析。
using Newtonsoft.Json; ?using System; ?using System.Collections.Generic; ?using System.Web.Mvc; ??namespace MyWebApp.Controllers ?{ ?????public class TestController : Controller ?????{ ?????????public ActionResult Index() ?????????{ ?????????????try ?????????????{ //比如说前端传过来的信息是jsonString ???????????????string jsonString = "[{\"name\":\"a\",\"value\":\"1\"},{\"name\":\"b\",\"value\":\"2\"}]"; ? string str=""; ???????????????List<kvp> objList = (List<kvp>)JsonConvert.DeserializeObject<List<kvp>>(jsonString ); foreach(var obj in objlist) { str=str+obj.name+"," } str=str.remove(str.length-1,1); } catch (Exception) { throw; } return View(str); } } ???public class kvp ???{ ?????????public string name { get; set; } ??????????public string value { get; set; } ??????}}asp.net MVC 框架中控制器里使用Newtonsoft.Json对前端传过来的字符串进行解析
原文地址:https://www.cnblogs.com/yuanfg/p/8963874.html