繁体   English   中英

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
}

使用此类模型:

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; }
}

您的班级结构错误

faces类应包含其他类属性

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中的其他类属性,依此类推

我建议您创建自定义“ JsonConverter”

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM