Web/ASP.NET MVC
[asp.net] json 문자를 커스텀 객체로 변환
RAY.D
2015. 4. 27. 14:04
Convert JSON string to custom Object.
http://forums.asp.net/t/1814643.aspx
Convert JSON string to custom Object.
Jun 15, 2012 11:23 AM|LINK
Hi all!
I have an Object:TestModels:
public class TestModels { public string Title { get; set; } public string Name { get; set; } }
When I using ajax to send data:
var objTest = {title: title, name: name}; $.ajax({ url: '/Controller/Action', type: 'POST', dataType: 'JSON', data: { test: JSON.stringify(objTest )}, success: function (data) { alert(data.Title); } });
and in Controller:
public JsonResult Action(string test) { JavaScriptSerializer objJavascript = new JavaScriptSerializer(); TestModels testModels = (TestModels) objJavascript.DeserializeObject(test); return Json(testModels); }
but, it not work!
Re: Convert JSON string to custom Object.
Jun 15, 2012 11:27 AM|LINK
please check this
http://james.newtonking.com/pages/json-net.aspx
http://www.codeproject.com/Tips/79435/Deserialize-JSON-with-C
Re: Convert JSON string to custom Object.
Jun 15, 2012 02:19 PM|LINK
Hello,
Have you tried having the TestModels object as ActionParameter, instead of string?
public JsonResult Action(TestModels model)
and change the javascript object
var model = { Title: title, Name: name };
and in ajax method
data: { model: model },
Maybe it works