Replace apostrophe in json string with empty string

Posted by user572844 on Stack Overflow See other posts from Stack Overflow or by user572844
Published on 2011-01-13T13:51:16Z Indexed on 2011/01/13 13:53 UTC
Read the original article Hit count: 310

Filed under:
|
|
|
|

Hi, I have problem with deserialization of json string, because string is bad format.

For example json object consist string property statusMessage with value "Hello "dog" ".

The correct format should be "Hello \" dog \" " .

I would like remove apostrophes from this property.

Something Like this. "Hello "dog" ". -> "Hello dog ".

Here is it original json string which I work.

"{\"jancl\":{\"idUser\":18438201,\"nick\":\"JANCl\",\"photo\":\"1\",\"sex\":1,\"photoAlbums\":1,\"videoAlbums\":0,\"sefNick\":\"jancl\",\"profilPercent\":75,\"emphasis\":false,\"age\":\"-\",\"isBlocked\":false,\"PHOTO\":{\"normal\":\"http://u.aimg.sk/fotky/1843/82/n_18438201.jpg?v=1\",\"medium\":\"http://u.aimg.sk/fotky/1843/82/m_18438201.jpg?v=1\",\"24x24\":\"http://u.aimg.sk/fotky/1843/82/s_18438201.jpg?v=1\"},\"PLUS\":{\"active\":false,\"activeTo\":\"0000-00-00\"},\"LOCATION\":{\"idRegion\":\"6\",\"regionName\":\"Trenciansky kraj\",\"idCity\":\"138\",\"cityName\":\"Trencianske Teplice\"},\"STATUS\":{\"isLoged\":true,\"isChating\":false,\"idChat\":0,\"roomName\":\"\",\"lastLogin\":1294925369},\"PROJECT_STATUS\":{\"photoAlbums\":1,\"photoAlbumsFavs\":0,\"videoAlbums\":0,\"videoAlbumsFavs\":0,\"videoAlbumsExts\":0,\"blogPosts\":0,\"emailNew\":0,\"postaNew\":0,\"clubInvitations\":0,\"dashboardItems\":1},\"STATUS_MESSAGE\":{\"statusMessage\":\"\"Status\"\",\"addTime\":\"1294872330\"},\"isFriend\":false,\"isIamFriend\":false}}"

Problem is here, json string consist this object:

"STATUS_MESSAGE": {"statusMessage":" "some "bad" value"   ", "addTime" :"1294872330"}

Condition of string which I want modified:

  • string start with "statusMessage":"
  • string can has any *lenght from 0 -N *
  • string end with ", "addTime

So I try write pattern for string which start with "statusMessage":", has any lenght and is ended with ", "addTime.

Here is it:

 const string pattern = "  \" statusMessage \" : \"  .*?  \",\"addTime\"  ";

 var regex = new Regex(pattern, RegexOptions.IgnoreCase);

//here i would replace " with empty string
 string result = regex.Replace(jsonString, match => ???);

But I think pattern is wrong, also I don’t know how replace apostrophe with empty string (remove apostrophne).

My goal is :

"statusMessage":" "some "bad" value"

to "statusMessage":" "some bad value"

Thank for advice

© Stack Overflow or respective owner

Related posts about c#

Related posts about regex