[asp.net] json 문자를 커스텀 객체로 변환
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.
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
'Web > ASP.NET MVC' 카테고리의 다른 글
[asp.net] json 문자를 json 객체로 파싱 (2) | 2015.04.27 |
---|---|
[asp.net] json 의 serialize(시리얼라이즈) / deserialize(디시리얼라이즈) 하기 (2) | 2015.04.27 |
[asp.net] Automapper to merge class (2) | 2015.04.27 |
[asp.net] MVC 에서 viewModel과 model 매핑 및 편집 방법 (2) | 2015.04.27 |
Introducing ASP.NET MVC 3 (Preview 1) (2) | 2015.04.27 |