Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace double quotes in json string with empty string

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 double quotes 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\":\"Trenčiansky kraj\",\"idCity\":\"138\",\"cityName\":\"Trenčianske 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 double quotes with empty string (remove double quotes).

My goal is :

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

to "statusMessage":" "some bad value"

Thank for advice


1 Answers

To serialize json on client side I use something like this:

    var JSON = JSON || {};  

    JSON.stringify = JSON.stringify || function (obj) {
        var t = typeof (obj);
        if (t != "object" || obj === null) {
            // simple data type  
            if (t == "string") obj = '"' + obj + '"';
            return String(obj);
        }
        else {
            // recurse array or object  
            var n, v, json = [], arr = (obj && obj.constructor == Array);
            for (n in obj) {
                v = obj[n]; t = typeof (v);
                if (t == "string") v = '"' + v + '"';
                else if (t == "object" && v !== null) v = JSON.stringify(v);
                json.push((arr ? "" : '"' + n + '":') + String(v));
            }
            return (arr ? "[" : "{") + String(json) + (arr ? "]" : "}");
        }
    };

then

$.ajax({
    ...
    data: JSON.stringify({
        someThing1: [
            { Id: '001', FooValue: ''},
            { Id: '002', FooValue: ''}
        ],
        someThing2: [
            { Id: '001', FooValue: ''},
            { Id: '002', FooValue: ''}
        ]
    }),
    ...
});

On server-side I use Newton.Json ( http://james.newtonking.com/pages/json-net.aspx )

object deserializeObject = JsonConvert.DeserializeObject(requestParameterTextRepresentation, RootType);

If you have no ability to modify client-side script to pass correct json-string, then all your regexps are vain effort.

like image 73
Dao Avatar answered Sep 23 '26 20:09

Dao



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!