1

i have a json string that i want to remap the parameter AOID to BOID

{  'ID': '56',  'AOID': 'o747'}

ive tried the following but i just got the same output

    public class CustomContractResolver : DefaultContractResolver
    {
        private Dictionary<string, string> PropertyMappings { get; set; }

        public CustomContractResolver()
        {
            this.PropertyMappings = new Dictionary<string, string>
            {
            { "AOID", "BOID"},
            };
        }

        protected override string ResolvePropertyName(string propertyName)
        {
            Console.WriteLine(propertyName);
            string resolvedName = null;
            var resolved = this.PropertyMappings.TryGetValue(propertyName, out resolvedName);
            return (resolved) ? resolvedName : base.ResolvePropertyName(propertyName);
        }
    }

    private void button2_Click(object sender, EventArgs e)
    {

        string product22 = "{  'ID': '56',  'AOID': '747'}";

        string json =
            JsonConvert.SerializeObject(product22,
                new JsonSerializerSettings { ContractResolver = new CustomContractResolver() }
                );
        Console.WriteLine(json);
    }

i get

"{  'ID': '56',  'AOID': '747'}"

but i am expecting to get

"{  'ID': '56',  'BOID': '747'}"

very new to c#....

thanks in advance

StuartAM
  • 15
  • 3
  • In your code you are calling the SerializeObject method and passing it a JSON string but the SerializeObject method is used to convert an object to a JSON string while the Deserialize method is used to convert the JSON string to an object. – quaabaam Aug 16 '19 at 23:45

3 Answers3

1

You could deserialise the object coming in to one class and then map that to another class.

As an example your first class would contain Id and AOID. This is the class you deserialise too. The second class would be ID and BOID and this would be the class you map it to.

Richard
  • 135
  • 1
  • 2
  • 10
0

Given that your product22 value is already serialized you can do a simple replace on a string like this:

    private void button2_Click(object sender, EventArgs e)
    {
        string product22 = "{  'ID': '56',  'AOID': '747'}";

        string json = product22.Replace("'AOID'", "'BOID'");

        Console.WriteLine(json);
    }

Hope it helps.

emt
  • 77
  • 8
  • i did think about doing it that way but in my larger dataset wasnt sure if it would take longer to run, i'll give it a go – StuartAM Aug 17 '19 at 06:10
0

You need to pass an object to your SerializeObject method call. You can build an anonymous object for your test like so...

object inputObject = new {ID = "56", AOID = "747"};

If you update your button click event to this you should get the results you are looking for...

private void button2_Click(object sender, EventArgs e)
{
    object inputObject = new {ID = "56", AOID = "747"}; // create anonymous object

    string json =
    JsonConvert.SerializeObject(inputObject,
    new JsonSerializerSettings { ContractResolver = new CustomContractResolver() });
    Console.WriteLine(json);
}
quaabaam
  • 1,808
  • 1
  • 7
  • 15