simplify this complex world

July 6, 2011

JSON Array Parsing Method in MVC Environment

Filed under: Web Retention — effendisusanto @ 3:01 am

Json usually play as a flexible and convenient form of data collection. In Json form, a collection of data packed as a single string so it’s easily transferred between several environment. With it’s simple form, json should make any transfer system easier. But yesterday I have to fight with array in json form(as I said in the first line – damn “USUALLY”-). I’ll try to explain the case so every one who face the same problem can understand and then solve their problem easily.

In my case I use JSON array to send a bunch of data from Javascript (View in MVC) to Controller. In Js I create a button to send json data


     $('#btnSave').click(function () {

       //var fromView = JSON.stringify(getLockVersion());
       var fromView= "[{"Code":"VER01","IsLocked":true},{"Code":"VER02","IsLocked":false},{"Code":"VER03","IsLocked":false}]";

       $.post('<%= Url.Action("UpdateLockVersion", "Maintenance") %>', { 'queryString': fromView }, function (data) {
           if (data.message != '') {

               alert (data.errorMessage));

                } else {

                    alert("Update Success");

                }

            });
        });

As shown in the source code I send data named ‘querystring’ which value is var string json array.
As all web developer know that JSON form usually like this

{"Code":"VER01","IsLocked":true}

But in this case I have send

     "[{"Code":"VER01","IsLocked":true},{"Code":"VER02","IsLocked":false},{"Code":"VER03","IsLocked":false}]"
     

It means I have to send several json form in serial and packed it in a single string data-type to method UpdateLockVersion in Maintenance controller. Now we have to see the content of this method

        
     public ActionResult UpdateLockVersion(string queryString)
        {
            var message = "";

            var ser = new JavaScriptSerializer();
            
            try
            {

                var versionLocks = ser.Deserialize<List<VersionLock>>(queryString);

                DataRepository.UpdateVersionList(versionLocks, out message);
            }

            catch(Exception ex)
            {

                message = ex.ToString();

                return Json(new { message });
            }

            return Json(new { message });
        }

In that I code there is list of VersionLock model, the content of VersionLock model is below

    public class VersionLock
    {
        public string Code { get; set; }
        public bool IsLocked { get; set; }
    }

As you see I have to use some strange code in my UpdateLockVersion method :


     var ser = new JavaScriptSerializer();
     var versionLocks = ser.Deserialize<List<VersionLock>>(queryString);

this method is .net native code, the JavaScriptSerializer() method is the member of using


System.Web.Script.Serialization;

So don’t forget to include this directive :D.
With this method I can unpack a serial/list of json array, Hope this can help anyone who face the same problem with me 😀

NB : THIS METHOD USE NO [AcceptVerbs(HttpVerbs.Post)] ATRIBUT

Blog at WordPress.com.