How to replace string using another string containing Json?

Asked

Viewed 412 times

2

Is there any way to replace a String with the fields of another string that contains a Json?

Example;

I got the string;

String template = "Olá [Nome], Tenha um bom dia ... hoje é [Data] e é seu aniversario";

And the other string would have;

String mensagem = "{"Nome": "Marconcilio","Data": "21/01/18"}"

Being in my string template the [Name] and [Date], are variables, ie at a given time can contain another data like [Password], but whenever this happens will have a respective field in the string that has Json (message).

In the end the result would be.

String conteudo = "Olá Marconcilio, Tenha um bom dia ... hoje é 21/01/18 e é seu aniversario";
  • how about using regex?

  • @Paz, can you do this with regex ? and replay the value that contains in Json ?

  • I believe so, just confirm me if the message value is correct and if it is, if it could be modified.

  • Do you want everything to be dynamic? Only having the string "template" and JSON?

  • @LINQ, that’s right. in Templete I have the fields that are in json.. and I want to replace them with the value of the Json property.

1 answer

2


You can deserialize JSON in a dictionary and then iterate over this dictionary to make substitutions in string of template.

Note that in my example, I use the package Newstonsoft.Json to deserialize the JSON with the answers. This is not necessary, you can use any other serializer, I chose this by custom. It makes no difference in the main idea.

string Transformar(string template, string json)
{
    var dict = JsonConvert.DeserializeObject<Dictionary<string, string>>(json); 

    string nova = template;
    foreach(var par in dict)
    {
        nova = nova.Replace($"[{par.Key}]", par.Value);
    }

    return nova;
}

void Main()
{
    string json = "{\"Nome\": \"Marconcilio\", \"Data\": \"21/01/18\"}";
    string template = "Olá [Nome], Tenha um bom dia ... hoje é [Data] e é seu aniversario";

    Console.WriteLine(template);
    // Olá [Nome], Tenha um bom dia ... hoje é [Data] e é seu aniversario
    Console.WriteLine(Transformar(template, json));
    // Olá Marconcilio, Tenha um bom dia ... hoje é 21/01/18 e é seu aniversario
}

See working on . NET Fiddle.

I made "marconcilio" a change to navigation Object in json.

string Transformar(string template, string json)
{
    var dict = JsonConvert.DeserializeObject<Dictionary<object, object>>(json); 


    string nova = template;
    foreach(var par in dict)
    {
        if(par.Value.ToString().Contains ("{"))
        {
            nova = Transformar(nova, par.Value.ToString());
        }

      nova = nova.Replace(string.Format("[{0}]", par.Key), par.Value.ToString());
    }

    return nova;
}

public void Main()
{
    string json = "{ \"Nome\": \"Marconcilio\",\"destinatarioDaNotificacao\": {\"Data\": 11},\"Notificacao\": {\"Teste\": \"novo\"}}";
    string template = "Olá [Nome], Tenha um bom dia ... hoje é [Data] e é seu aniversario .. [Teste]";

    Console.WriteLine(template);
    Console.WriteLine(Transformar(template, json));
}
  • which use is required for Jsonconvert to work?

  • 1

    @Peace I think my edition already answers this

  • I amended your reply with an amendment I needed.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.