簡體   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