Skip to main content
se añadieron 858 caracteres en el cuerpo
Origen Enlace
Leandro Tuttini
  • 36k
  • 3
  • 15
  • 41

Si necesitas deserializar de forma generica, puedes usar el JObject

Parsing JSON Object using JObject.Parse

string json = @"{
  CPU: 'Intel',
  Drives: [
    'DVD read/writer',
    '500 gigabyte hard drive'
  ]
}";

JObject o = JObject.Parse(json);

se puede deserializar sin un objeto

Using JSON.NET for dynamic JSON parsing

analiza el titulo "Importing JSON with JObject.Parse() and JArray.Parse()" pero basicamente define

var jsonString = @"{""Name"":""Rick"",""Company"":""West Wind"",
                    ""Entered"":""2012-03-16T00:03:33.245-10:00""}";

dynamic json = JValue.Parse(jsonString);

string name = json.Name;
string company = json.Company;
DateTime entered = json.Entered;

al usar dynamic no tendrias problema en deserializar el json sin una clase

Si usas el JObject puedes acceder a los nodos

Parse JSON object in C# with different value types

string sampleJson = "{\"results\":[" +
    "{\"employeename\":\"name1\",\"employeesupervisor\":\"supervisor1\"}," +
    "{\"employeename\":\"name2\",\"employeesupervisor\":\"supervisor1\"}," +
    "{\"employeename\":\"name3\",\"employeesupervisor\":[\"supervisor1\",\"supervisor2\"]}" +
    "]}";

// Parse JSON into dynamic object, convenient!
JObject results = JObject.Parse(sampleJson);

// Process each employee
foreach (var result in results["results"])
{
    // this can be a string or null
    string employeeName = (string)result["employeename"];

  //resto codigo

En resumen podrias usar el JObject o dynamic para deserializar sin una clase con la cual mapear el json

Si necesitas deserializar de forma generica, puedes usar el JObject

Parsing JSON Object using JObject.Parse

string json = @"{
  CPU: 'Intel',
  Drives: [
    'DVD read/writer',
    '500 gigabyte hard drive'
  ]
}";

JObject o = JObject.Parse(json);

se puede deserializar sin un objeto

Using JSON.NET for dynamic JSON parsing

analiza el titulo "Importing JSON with JObject.Parse() and JArray.Parse()" pero basicamente define

var jsonString = @"{""Name"":""Rick"",""Company"":""West Wind"",
                    ""Entered"":""2012-03-16T00:03:33.245-10:00""}";

dynamic json = JValue.Parse(jsonString);

string name = json.Name;
string company = json.Company;
DateTime entered = json.Entered;

al usar dynamic no tendrias problema en deserializar el json sin una clase

Si necesitas deserializar de forma generica, puedes usar el JObject

Parsing JSON Object using JObject.Parse

string json = @"{
  CPU: 'Intel',
  Drives: [
    'DVD read/writer',
    '500 gigabyte hard drive'
  ]
}";

JObject o = JObject.Parse(json);

se puede deserializar sin un objeto

Using JSON.NET for dynamic JSON parsing

analiza el titulo "Importing JSON with JObject.Parse() and JArray.Parse()" pero basicamente define

var jsonString = @"{""Name"":""Rick"",""Company"":""West Wind"",
                    ""Entered"":""2012-03-16T00:03:33.245-10:00""}";

dynamic json = JValue.Parse(jsonString);

string name = json.Name;
string company = json.Company;
DateTime entered = json.Entered;

al usar dynamic no tendrias problema en deserializar el json sin una clase

Si usas el JObject puedes acceder a los nodos

Parse JSON object in C# with different value types

string sampleJson = "{\"results\":[" +
    "{\"employeename\":\"name1\",\"employeesupervisor\":\"supervisor1\"}," +
    "{\"employeename\":\"name2\",\"employeesupervisor\":\"supervisor1\"}," +
    "{\"employeename\":\"name3\",\"employeesupervisor\":[\"supervisor1\",\"supervisor2\"]}" +
    "]}";

// Parse JSON into dynamic object, convenient!
JObject results = JObject.Parse(sampleJson);

// Process each employee
foreach (var result in results["results"])
{
    // this can be a string or null
    string employeeName = (string)result["employeename"];

  //resto codigo

En resumen podrias usar el JObject o dynamic para deserializar sin una clase con la cual mapear el json

Origen Enlace
Leandro Tuttini
  • 36k
  • 3
  • 15
  • 41

Si necesitas deserializar de forma generica, puedes usar el JObject

Parsing JSON Object using JObject.Parse

string json = @"{
  CPU: 'Intel',
  Drives: [
    'DVD read/writer',
    '500 gigabyte hard drive'
  ]
}";

JObject o = JObject.Parse(json);

se puede deserializar sin un objeto

Using JSON.NET for dynamic JSON parsing

analiza el titulo "Importing JSON with JObject.Parse() and JArray.Parse()" pero basicamente define

var jsonString = @"{""Name"":""Rick"",""Company"":""West Wind"",
                    ""Entered"":""2012-03-16T00:03:33.245-10:00""}";

dynamic json = JValue.Parse(jsonString);

string name = json.Name;
string company = json.Company;
DateTime entered = json.Entered;

al usar dynamic no tendrias problema en deserializar el json sin una clase