As I understand it, what you want to do (in C #) is to receive a JSON and save it in a type object
class Persona{
string Nombre = "Juan";
string Apellido = "Luna";
int Edad = 26;
DateTime FechaNacimiento = "09/03/1990";
}
If that is the case, do not break your head; there is the library Json de Newtonsoft that processes the Json for you, you just have to go to your documentation to experiment and see what the solution is.
From the outset I tell you that you need to have an object like the one above but with get
and set
:
public class Persona{
public string Nombre { get; set; }
public string Apellido { get; set; }
public int Edad { get; set; }
public DateTime FechaNacimiento { get; set; }
}
and then treat your Json with JObject
:
var personaIntermedioObject = JObject.Parse(el_json);
to finally assign the data% Persona
:
Persona persona = new Persona(){
Nombre = personaIntermedioObject["Nombre"].ToString(),
Apellido = personaIntermedioObject["Apellido"].ToString(),
Edad = Convert.ToInt32(personaIntermedioObject["Edad"]),
FechaNacimiento = (DateTime)personaIntermedioObject["FechaNacimiento"]
}
Or try to pair it directly with
Persona persona = JsonConvert.DeserializeObject<Persona>(el_json);
EDIT:
I think I understand your question. What you will receive is a JSON that can be any structure, not just the one you gave example, style
{nombre:"Juan",edad:"24"}
{estado:"Guanajuato",ciudad:"Celaya"}
and show them (or use them) as
Propiedad | Valor Propiedad | Valor
--------------------- -----------------------
nombre | Juan estado | Guanajuato
edad | 24 ciudad | Celaya
In that case, following with Json de Newtonsoft, you need a cycle that runs through each of the pairs of Key: Value of the Json. An example that stores the data in a dictionary is:
System.Collections.Generic.Dictionary<string, object> dic = new System.Collections.Generic.Dictionary<string, object>();
foreach(JProperty obj in personaIntermedioObject.Properties())
{
dic.Add(obj.Name, obj.Value);
// O bien, imprimirlos en consola:
Console.WriteLine("Clave: {0} | Valor: {1}", obj.Name, obj.Value.ToString())
}
That way, you can store the information that is, whatever its size, in a structure that you can use later.
My reference: link
EDIT 2:
As far as I know, you can not create variables "on the fly", the closest thing you can do is use an object dynamic
as follows:
dynamic DynObj = JObject.Parse(el_json);
That way, you can consult about DynObj.Nombre.ToString()
or DynObj.estado.ToString()
.
Also, why do you want to create these data as variables if you can freely browse all the data with a dictionary? Take into account that even with web pages (what I guess you do for your Response
), before serving the aspx
or the cshtml
this has to be compiled, you can not be adding the variables as well.
Read about the topic:
link
link
link
link