Newtonsoft.Json is used in the controller in the asp.net MVC framework to parse the string passed from the front end

Next, I will share my experience with you with an example. Newtonsoft.Json is used in the controller in the asp.net MVC framework to parse the string passed from the front end.

using Newtonsoft.Json;  
using System;  
using System.Collections.Generic;  
using System.Web.Mvc; 
  
namespace MyWebApp.Controllers  
{  
    public class TestController : Controller  
    {  
        public ActionResult Index()  
        {  
            try  
            {
          // For example, the information from the front end is 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; }   
    }
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324945882&siteId=291194637