Json.Net se ha convertido en una biblioteca muy útil para poder hacer uso de JSON, entre las funciones de la biblioteca se encuentra el hecho de serializar y deserializar clases.
En este ejemplo vamos a suponer que tenemos un JSON y queremos serializarlo para poder manipularlo, ¿Cómo haremos esto? es sencillo, consideremos el siguiente JSON
[{"faceId":"bbb1c3d3-7e0b-4d03-826e-b5cef7afc660","faceRectangle":{"top":241,"left":271,"width":187,"height":187},"faceAttributes":{"smile":0.0,"headPose":{"pitch":-0.4,"roll":-2.2,"yaw":7.7},"gender":"male","age":40.0,"facialHair":{"moustache":0.6,"beard":0.4,"sideburns":0.1},"glasses":"ReadingGlasses","emotion":{"anger":0.0,"contempt":0.001,"disgust":0.0,"fear":0.0,"happiness":0.0,"neutral":0.984,"sadness":0.015,"surprise":0.0},"blur":{"blurLevel":"low","value":0.24},"exposure":{"exposureLevel":"goodExposure","value":0.66},"noise":{"noiseLevel":"low","value":0.01},"makeup":{"eyeMakeup":false,"lipMakeup":true},"accessories":[{"type":"glasses","confidence":0.99}],"occlusion":{"foreheadOccluded":false,"eyeOccluded":false,"mouthOccluded":false},"hair":{"bald":0.17,"invisible":false,"hairColor":[{"color":"brown","confidence":0.99},{"color":"black","confidence":0.81},{"color":"gray","confidence":0.31},{"color":"blond","confidence":0.25},{"color":"red","confidence":0.13},{"color":"other","confidence":0.09}]}}}]
El primer paso es obtener las clases correspondientes a el, esto lo podemos hacer de forma manual o entrar en la pagina:
http://json2csharp.com/
Y en esta pagina vamos a colocar el JSON generándonos las clases necesarias
public class FaceRectangle
{
public int top { get; set; }
public int left { get; set; }
public int width { get; set; }
public int height { get; set; }
}
public class HeadPose
{
public double pitch { get; set; }
public double roll { get; set; }
public double yaw { get; set; }
}
public class FacialHair
{
public double moustache { get; set; }
public double beard { get; set; }
public double sideburns { get; set; }
}
public class Emotion
{
public double anger { get; set; }
public double contempt { get; set; }
public double disgust { get; set; }
public double fear { get; set; }
public double happiness { get; set; }
public double neutral { get; set; }
public double sadness { get; set; }
public double surprise { get; set; }
}
public class Blur
{
public string blurLevel { get; set; }
public double value { get; set; }
}
public class Exposure
{
public string exposureLevel { get; set; }
public double value { get; set; }
}
public class Noise
{
public string noiseLevel { get; set; }
public double value { get; set; }
}
public class Makeup
{
public bool eyeMakeup { get; set; }
public bool lipMakeup { get; set; }
}
public class Occlusion
{
public bool foreheadOccluded { get; set; }
public bool eyeOccluded { get; set; }
public bool mouthOccluded { get; set; }
}
public class HairColor
{
public string color { get; set; }
public double confidence { get; set; }
}
public class Hair
{
public double bald { get; set; }
public bool invisible { get; set; }
public List<HairColor> hairColor { get; set; }
}
public class FaceAttributes
{
public double smile { get; set; }
public HeadPose headPose { get; set; }
public string gender { get; set; }
public double age { get; set; }
public FacialHair facialHair { get; set; }
public string glasses { get; set; }
public Emotion emotion { get; set; }
public Blur blur { get; set; }
public Exposure exposure { get; set; }
public Noise noise { get; set; }
public Makeup makeup { get; set; }
public List<object> accessories { get; set; }
public Occlusion occlusion { get; set; }
public Hair hair { get; set; }
}
public class RootObject
{
public string faceId { get; set; }
public FaceRectangle faceRectangle { get; set; }
public FaceAttributes faceAttributes { get; set; }
}
Estas clases las agregamos a nuestro proyecto.
Ahora únicamente efectuamos esta llamada
List<RootObject> caras= JsonConvert.DeserializeObject<List<RootObject>>(contentString);
Felices lineas
En este ejemplo vamos a suponer que tenemos un JSON y queremos serializarlo para poder manipularlo, ¿Cómo haremos esto? es sencillo, consideremos el siguiente JSON
[{"faceId":"bbb1c3d3-7e0b-4d03-826e-b5cef7afc660","faceRectangle":{"top":241,"left":271,"width":187,"height":187},"faceAttributes":{"smile":0.0,"headPose":{"pitch":-0.4,"roll":-2.2,"yaw":7.7},"gender":"male","age":40.0,"facialHair":{"moustache":0.6,"beard":0.4,"sideburns":0.1},"glasses":"ReadingGlasses","emotion":{"anger":0.0,"contempt":0.001,"disgust":0.0,"fear":0.0,"happiness":0.0,"neutral":0.984,"sadness":0.015,"surprise":0.0},"blur":{"blurLevel":"low","value":0.24},"exposure":{"exposureLevel":"goodExposure","value":0.66},"noise":{"noiseLevel":"low","value":0.01},"makeup":{"eyeMakeup":false,"lipMakeup":true},"accessories":[{"type":"glasses","confidence":0.99}],"occlusion":{"foreheadOccluded":false,"eyeOccluded":false,"mouthOccluded":false},"hair":{"bald":0.17,"invisible":false,"hairColor":[{"color":"brown","confidence":0.99},{"color":"black","confidence":0.81},{"color":"gray","confidence":0.31},{"color":"blond","confidence":0.25},{"color":"red","confidence":0.13},{"color":"other","confidence":0.09}]}}}]
El primer paso es obtener las clases correspondientes a el, esto lo podemos hacer de forma manual o entrar en la pagina:
http://json2csharp.com/
Y en esta pagina vamos a colocar el JSON generándonos las clases necesarias
public class FaceRectangle
{
public int top { get; set; }
public int left { get; set; }
public int width { get; set; }
public int height { get; set; }
}
public class HeadPose
{
public double pitch { get; set; }
public double roll { get; set; }
public double yaw { get; set; }
}
public class FacialHair
{
public double moustache { get; set; }
public double beard { get; set; }
public double sideburns { get; set; }
}
public class Emotion
{
public double anger { get; set; }
public double contempt { get; set; }
public double disgust { get; set; }
public double fear { get; set; }
public double happiness { get; set; }
public double neutral { get; set; }
public double sadness { get; set; }
public double surprise { get; set; }
}
public class Blur
{
public string blurLevel { get; set; }
public double value { get; set; }
}
public class Exposure
{
public string exposureLevel { get; set; }
public double value { get; set; }
}
public class Noise
{
public string noiseLevel { get; set; }
public double value { get; set; }
}
public class Makeup
{
public bool eyeMakeup { get; set; }
public bool lipMakeup { get; set; }
}
public class Occlusion
{
public bool foreheadOccluded { get; set; }
public bool eyeOccluded { get; set; }
public bool mouthOccluded { get; set; }
}
public class HairColor
{
public string color { get; set; }
public double confidence { get; set; }
}
public class Hair
{
public double bald { get; set; }
public bool invisible { get; set; }
public List<HairColor> hairColor { get; set; }
}
public class FaceAttributes
{
public double smile { get; set; }
public HeadPose headPose { get; set; }
public string gender { get; set; }
public double age { get; set; }
public FacialHair facialHair { get; set; }
public string glasses { get; set; }
public Emotion emotion { get; set; }
public Blur blur { get; set; }
public Exposure exposure { get; set; }
public Noise noise { get; set; }
public Makeup makeup { get; set; }
public List<object> accessories { get; set; }
public Occlusion occlusion { get; set; }
public Hair hair { get; set; }
}
public class RootObject
{
public string faceId { get; set; }
public FaceRectangle faceRectangle { get; set; }
public FaceAttributes faceAttributes { get; set; }
}
Estas clases las agregamos a nuestro proyecto.
Ahora únicamente efectuamos esta llamada
List<RootObject> caras= JsonConvert.DeserializeObject<List<RootObject>>(contentString);
Felices lineas
No hay comentarios.:
Publicar un comentario