[asp.net] json 문자를 커스텀 객체로 변환

Posted by RAY.D
2015. 4. 27. 14:04 Web/ASP.NET MVC
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