Buscar contenidos

viernes, 1 de noviembre de 2019

Deserialize JSON into C# dynamic object


https://stackoverflow.com/questions/3142495/deserialize-json-into-c-sharp-dynamic-object



dynamic stuff = JsonConvert.DeserializeObject("{ 'Name': 'Jon Smith', 'Address': { 'City': 'New York', 'State': 'NY' }, 'Age': 42 }");

                string name = stuff.Name;

                string address = stuff.Address.City;



-----



Simple "string JSON data" to object without any third-party DLL file:
WebClient client = new WebClient();
string getString = client.DownloadString("https://graph.facebook.com/zuck");

JavaScriptSerializer serializer = new JavaScriptSerializer();
dynamic item = serializer.Deserialize<object>(getString);
string name = item["name"];

//note: JavaScriptSerializer in this namespaces
//System.Web.Script.Serialization.JavaScriptSerializer
Note: You can also using your custom object.
Personel item = serializer.Deserialize<Personel>(getString);

No hay comentarios:

Publicar un comentario