Replace the integers of a json with strings C #

0

Hello friends, I need to rewind all the integers within a json by strings, to say that if I have an object like:

  

{x: 1} ... I'll want to have {x: "1"}

but I need you to do it with a json of any size, such as

  

{"x": 2, "y": "GLAN105", "z": "2042"}, {"x": 4, "y": "GLAN425", "z": "2042"} , {"x": 34, "and": "ZLXN425", "z": "2212"}, etc ...

I do not know if there is any function or something similar that I do with this style in C #?

    
asked by E.Rawrdríguez.Ophanim 14.09.2018 в 00:54
source

1 answer

1

Get the keys of your JSON and replace its value with strings

string jsonprueba = "{\"A\": 1,\"B\": 2,\"C\": 3}";
JObject jsonObj = (JObject)Newtonsoft.Json.JsonConvert.DeserializeObject(jsonprueba);
var keys = jsonObj.Properties().Select(p => p.Name).ToList(); 
foreach(string k in keys)
{
    jsonObj[k] = jsonObj[k].ToString();
}
var jst=jsonObj.ToString();
// IN:  {"A": 1,"B": 2,"C": 3}
// OUT: {"A": "1","B": "2","C": "3"}
    
answered by 14.09.2018 / 01:24
source