asp.net MVC 框架中控制器里使用Newtonsoft.Json对前端传过来的字符串进行解析

下面我用一个实例来和大家分享一下我的经验,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; }   
    }
}

 

posted @ 2018-04-27 18:25  €键盘人生  阅读(256)  评论(0编辑  收藏  举报
医疗信息化系统