Yacine Ben Slimene
Yacine Ben Slimene

Reputation: 135

Smartly replace strings

I am working with JSON API. As c# doesn't accept characters like - (minus) or . (point), I had to replace each character by _ (underscore). The replacement happens when the JSON response is received as a string so that every attribute name containing a - or a . will have it replaced by a _ , then every attribute name will be the same as the attributes names in the class it will be deserialized into.

To make it clearer, here are some examples:

I recieve the following JSON : { "id": 1, "result": [ { "data": [ { "adm-pass": ""}]} In the class I want to deserialize into I have this attribute : public String adm_pass {get; set;}

So I replace the minus with an underscore so that the NewtonSoft parser can deserialize it accordingly.

My problem is that I sometimes I get some negative integers in my JSON. So if I do the string replacement in: {"beta" : -1}, I get a parsing exception since the -1 (integer here) becomes _1 and cannot be deserialized properly and raises an exception.

Is there a way to replace the string smartly so I can avoid this error? For example if - is followed by an int it's not replaced. If this way does not exist, is there a solution for this kind of problems?

Upvotes: 0

Views: 194

Answers (4)

Alfie Goodacre
Alfie Goodacre

Reputation: 2793

You can substring to check whether the next character is an integer, this can adapt into your code easily as you already find a character, as such you could do

int a;

if(int.TryParse(adm_pass.Substring(adm_pass.IndexOf("-") + 1,1),out a))
{
    //Code if next character is an int
}
else
{
    adm_pass = adm_pass.Replace("-","_");
}

This kind of code can be looped until there are no remaining hyphens/minuses

Upvotes: 0

Rhyous
Rhyous

Reputation: 6690

Regex could work as wlalele suggests.

But I would create a new object like this:

  1. Create a new object:

    var sharpObj = {};
    
  2. loop through the objects as properties as described here: Iterate through object properties

    for (var property in object) {
        if (object.hasOwnProperty(property)) {
            // do stuff
        }
    }
    
  3. In the // do stuff section, create a property on sharpObj with the desired string replacements and set the property to the same value.

    var cleanProperty = cleanPropertyName(property);
    sharpObj[cleanProperty] = orginalObject[property];
    

Note: I assume you can figure out the cleanPropertyName() method or similar.

  1. Stringify the object

    var string = JSON.stringify(sharpObj);
    

Upvotes: 1

Ferdin
Ferdin

Reputation: 362

Newtonsoft allows you to specify the exact name of the JSON property, which it will use to serialize/deserialize. So you should be able to do this

[JsonProperty("adm-pass")]
public String adm_pass { get; set; }

This way you are not restricted to name your properties exactly as the JSON property names. And in your case, you won't need to do a string replace.

Hope this helps.

Upvotes: 4

wlalele
wlalele

Reputation: 333

You'll have to check that you are replacing the key and not the value, maybe by using a regex like http://regexr.com/3d471

Upvotes: 2

Related Questions