You must Sign In to post a response.
  • Category: Webservices

    How to deserialize to a list object in web api

    Hi All,

    I have receive an data in following format of json, it is contain like \r\n

    {
    "Status": true,
    "Message": "Success.",
    "Data": null,
    "RecordCount": 4,
    "Collection": [
    {
    "Bsid": "1-XY01CC58-1",
    "Code": "WEB\r\n",
    "SourceName": "Web Campaign\r\n",
    "Description": "b n nmjn vb nmjbv nmjbv",
    "IsActive": true,
    "PropertyId": null,
    "UserName": null
    }
  • #766272
    //Replace escape sequences.

    jsonString = jsonString.Replace(@"\r\n", " ");

    //Deserialize if you are using json.NET.

    SearchRootObject obj = JsonConvert.DeserializeObject<SearchRootObject>(jsonString);

    //Or With JavaScriptSerializer Class.

    JavaScriptSerializer().Deserialize<SearchObj.RootObject>(jsonString);

  • #766273
    Many time JSON return '\r\n' characters, basically you need to Take your JSON and .stringify() it. Then use the .replace() method and replace all occurrences of \n with \\n.
    I think there is no inbuilt library for escaping all special characters in a string, you need to customize your code as follows

    var myJSONString = JSON.stringify(myJSON);
    var myEscapedJSONString = myJSONString.replace(/\\n/g, "\\n")
    .replace(/\\'/g, "\\'")
    .replace(/\\"/g, '\\"')
    .replace(/\\&/g, "\\&")
    .replace(/\\r/g, "\\r")
    .replace(/\\t/g, "\\t")
    .replace(/\\b/g, "\\b")
    .replace(/\\f/g, "\\f");
    // myEscapedJSONString is now ready to be POST'ed to the server.

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #766274
    Hi,
    Just replace '\n and \r' with '' i.e. blank values, try this:

    var responseText = received data;
    var finalJSONResp = responseText.replace(/\\n/g, "")
    .replace(/\\r/g, "");
    alert(finalJSONResp);

  • #766275
    Hi
    you can go through Below links


    "asp.net/web-api/overview/formats-and-model-binding/json-and-xml-serialization"
    "stackoverflow.com/questions/15841309/deserializing-list-of-json-objects-in-webapi-method"


    Name : Dotnet Developer-2015
    Email Id : kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #766294
    Hi All of you, i want to serialize or deserialize without replace word, any other way of doing deserialize a list or collection.

    Thanks & Regards,

    Atul Sharma


  • Sign In to post your comments