c# RESTSHARP 反序列化 json

c# RESTSHARP Deserialize json

我如何对 json 以下的二维格式执行反序列化?我已经寻找了许多解决方案并尝试应用但失败了。下面是我的代码,能告诉我哪一部分是错的吗??

我使用rest api请求客户端,响应数据是json格式,我想进一步分解得到年龄、性别、身份信息等等。我该如何执行?提前致谢

class Program
    {
        static void Main(string[] args)
        {
            var client = new RestClient("https://watson-api-explorer.mybluemix.net/visual-recognition/api/v3/detect_faces?api_key=dcd38b28dee23fe70a261421785a0ff6983dfe08&url=https%3A%2F%2Fpbs.twimg.com%2Fprofile_images%2F526212687101820929%2FwmAKmxjb.jpeg&version=2017-11-06 ");
            var request = new RestRequest(Method.GET);
            request.AddHeader("Accept", "application/json");
            request.Parameters.Clear();
            request.AddParameter("application/json", ParameterType.RequestBody);
            request.RequestFormat = DataFormat.Json;

            IRestResponse response = client.Execute(request);
            var obj = JsonConvert.DeserializeObject<Rootobject>(response.Content);

            Debug.WriteLine("age===");
            Debug.WriteLine("ageMax===");
            Debug.WriteLine("ageMin===");
            Debug.WriteLine("ageScore===");
        }
    }
    public class Rootobject
    {
        public Result[] images { get; set; }
    }

    public class Result
    {
        public faces[] faces { get; set; }
    }

    public class faces
    {

        public string age { get; set; }
        public string max { get; set; }
        public string min { get; set; }
        public string score { get; set; }
    }

JSON 格式

{
    "images": [
        {
            "faces": [
                {
                    "age": {
                        "max": 64,
                        "min": 55,
                        "score": 0.408852
                    },
                    "face_location": {
                        "height": 759,
                        "left": 243,
                        "top": 151,
                        "width": 597
                    },
                    "gender": {
                        "gender": "MALE",
                        "score": 0.99593
                    },
                    "identity": {
                        "name": "Najib Razak",
                        "score": 0.99593
                    }
                }
            ],
            "resolved_url": "https://pbs.twimg.com/profile_images/526212687101820929/wmAKmxjb.jpeg",
            "source_url": "https://pbs.twimg.com/profile_images/526212687101820929/wmAKmxjb.jpeg"
        }
    ],
    "images_processed": 1
}

我建议您创建自定义 "JsonConverter"

这是一个例子: https://www.newtonsoft.com/json/help/html/CustomJsonConverter.htm

您将完全控制将 json 反序列化为您想要的 POCO 的过程。

你的class结构有误

面部 class 应包含其他 class 属性

public class Faces
{
    public Age age {get;set;}
    public FaceLocation face_location {get;set;}
    public Gender gender {get;set;}
    public Identity identity {get;set;}
}
public class Age 
{
    public int max {get;set;}
    public int min {get;set;}
    public double score {get;set;}
}

对于 Faces 中的其他 class 属性依此类推

使用这个class模型:

using Newtonsoft.Json;

public class RootObject
{
    [JsonProperty("images")]
    public Image[] Images { get; set; }

    [JsonProperty("images_processed")]
    public long ImagesProcessed { get; set; }
}

public class Image
{
    [JsonProperty("faces")]
    public Face[] Faces { get; set; }

    [JsonProperty("resolved_url")]
    public string ResolvedUrl { get; set; }

    [JsonProperty("source_url")]
    public string SourceUrl { get; set; }
}

public class Face
{
    [JsonProperty("age")]
    public Age Age { get; set; }

    [JsonProperty("face_location")]
    public FaceLocation FaceLocation { get; set; }

    [JsonProperty("gender")]
    public Gender Gender { get; set; }

    [JsonProperty("identity")]
    public Identity Identity { get; set; }
}

public class Identity
{
    [JsonProperty("name")]
    public string Name { get; set; }

    [JsonProperty("score")]
    public double Score { get; set; }
}

public class Gender
{
    [JsonProperty("gender")]
    public string PurpleGender { get; set; }

    [JsonProperty("score")]
    public double Score { get; set; }
}

public class FaceLocation
{
    [JsonProperty("height")]
    public long Height { get; set; }

    [JsonProperty("left")]
    public long Left { get; set; }

    [JsonProperty("top")]
    public long Top { get; set; }

    [JsonProperty("width")]
    public long Width { get; set; }
}

public class Age
{
    [JsonProperty("max")]
    public long Max { get; set; }

    [JsonProperty("min")]
    public long Min { get; set; }

    [JsonProperty("score")]
    public double Score { get; set; }
}