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

    How to parse json in asp.net

    Hello friends,
    I am in process to parse json.

    Once I store json in string, I get following string

    Json is


    ":[
    {"firstName":"John", "lastName":"Doe"},
    {"firstName":"Anna", "lastName":"Smith"},
    {"firstName":"Peter", "lastName":"Jones"}
    ]}


    It converts i c# string as


    string s="\":[\r\n {\"firstName\":\"John\", \"lastName\":\"Doe\"},\r\n {\"firstName\":\"Anna\", \"lastName\":\"Smith\"},\r\n {\"firstName\":\"Peter\", \"lastName\":\"Jones\"}\r\n]}\r\n\r\n"


    Although \r\n can be removed using

    string m = s.Replace(System.Environment.NewLine, string.Empty);

    But how to remove \ from whole string.

    "\":[ {\"firstName\":\"John\", \"lastName\":\"Doe\"}, {\"firstName\":\"Anna\", \"lastName\":\"Smith\"}, {\"firstName\":\"Peter\", \"lastName\":\"Jones\"}]}"


    Please share any code snippet if u have.


    Thanks and Regards
    Navneet
  • #763725
    Hai Navneet,
    You can use the code below to replace the '\' with the empty string as:

    string k = m.Replace('\',' ');

    Hope it will be helpful to you.

    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com

  • #763734
    simple, just use replace method

    s = s.Replace(@"\", string.Empty);
    OR
    s = s.Replace("\\","");

    pl. let us know if you face any problem

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

  • #763751
    Hi,

    I guess Prasad answer will help you to remove the back slash, if you still have issues then please let us know.

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #763813
    Following is the valid JSON

    {{"firstName":"John", "lastName":"Doe"},
    {"firstName":"Anna", "lastName":"Smith"},
    {"firstName":"Peter", "lastName":"Jones"}}

    By Nathan
    Direction is important than speed


Sign In to post your comments