Modify the Json fomato with C # in .Net

1

I have a code that serializes and converts the fields of a database to JSON , through JsonConvert , and the problem is that I want to modify the normal format of JSON , which follows the structure as in this link .

Here is the code part, but what it does is that it generates a% normal JSON , but I do not know how to modify it, according to the link:

C #

public class DatosCatPuestoGeneral
{
    public List<arrDatosCatPuestoGeneral> DatosCatalogoPuestoGeneral = new List<arrDatosCatPuestoGeneral>();
    public string toJson()
    {
        return Newtonsoft.Json.JsonConvert.SerializeObject(this);
    }
}
    
asked by Guillermo Navarro 23.09.2016 в 05:44
source

2 answers

1

To have that result, you need a class with those fields.

class Persona {

   public string Name { get; set; }
   public string Email { get; set; }

}

Then you must populate an Array or List with elements of that class, by whatever method.

var personas = new ArrayList();
personas.Add(new Persona { Nomre = "Juan", Email = "[email protected]" });
personas.Add(new Persona { Nomre = "Jose", Email = "[email protected]" });

Finally you serialize it to Json.

var json = Newtonsoft.Json.JsonConvert.SerializeObject(personas);
    
answered by 23.09.2016 в 06:13
0

You can modify it using the Newtonsoft for .Net

libraries

An example would be as follows:

string json = JsonConvert.SerializeObject(ClaseParaSerializar);
JObject datosParaModificarEnElJson = JObject.Parse(json);
datosParaModificarEnElJson["TransaccionId"] = 123456;
JObject objeto = (JObject)datosParaModificarEnElJson["datosParaModificarEnElJson"];
objeto.Remove("AlgunosDatosParaEliminar");
json = datosParaModificarEnElJson.ToString();

You can directly access the nodes of the Json and from there modify the values and again re-serialize the Json

    
answered by 12.10.2016 в 20:45