Search code examples
wcfjson.netjquery-jtable

Jtable field options not display json because it contains /(slashes)


My wcf project returns the Countries List as json, in wcf project parse using Newtonsoft json. I get json in my client website built on mvc, i show the data using jtable plugin in jquery. The plugin failed to display because, the json contains /(back slashes).

Code to display jtable is

 Name:{
                    title: 'Country Name',
                    width: '40%',
                    options: '/Country/Index'
}

Country/Index result is

{"Result":"OK","Options":"[{\"DisplayText\":\"India\",\"Value\":1},{\"DisplayText\":\"Singapore\",\"Value\":2}]"}

Country/Index gets the result from wcf service. Is there any way to strip the / in json.

Edit: My wcf code is

 List<JSONDisplayNameValue> lstLanguages = new List<JSONDisplayNameValue>();
//Get data from db. 
//JSONDisplayNameValue has two variables DisplayName, Value
  var json = JsonConvert.SerializeObject(lstLanguages);
        return  json;

Country/Index mvc code is

public JsonResult Index()
    {
        string Countries_list = processsvc.GetAllCountries();
        return Json(new { Result = "OK", Options = Countries_list },  JsonRequestBehavior.AllowGet);
    }

Solution

  • You are getting backslashes because you are double-serializing your data.

    • In your WCF code, you call JsonConvert.SerializeObject() to serialize your list of countries/languages to JSON before returning it.
    • In your MVC controller, you call your WCF service to get the list, which is already in JSON format. But then you add that JSON to another object and call Json() which causes the list to be serialized a second time. That is where the backslashes get added.

    To fix this, you either need to

    • Deserialize the list you get back from your WCF service before adding it to the MVC result.

      -- OR --

    • Make a way to retrieve your list of countries/languages without serializing it to JSON. Then add the unserialized list to your MVC result and serialize it as normal.