.net 如何将Web API响应转换为model.data格式?

x4shl7ld  于 2023-03-31  发布在  .NET
关注(0)|答案(1)|浏览(86)

[my Web API响应]
我正在尝试将Web API响应转换为模型类数据。使用JsonConvert.DeserializeObject但出现错误无法将当前JSON数组(例如[1,2,3])反序列化为类型“Demo.Models.picture_list”,因为该类型需要JSON对象(例如{“name”:“value”})才能正确反序列化。我创建了名为PictureViewModel的模型类,并使用JsonConvert.DeserializeObject(apiResponse)将响应转换为Class数据格式以检索响应的图像。我分享了我的代码:

public class ProductViewModel
    {
        public int album_id { get; set; }
        public string album_name { get; set; }
        public bool is_face_detection { get; set; }
        public string message { get; set; }
        public bool status { get; set; }
        public bool has_next_page { get; set; }
        public int total_images { get; set; }
        public int selected_images { get; set; }
        public string large_thumb_width { get; set; }
        public picture_list picture_list { get; set; }   
    }

   public class picture_list
    {
        public int PictureId { get; set; }
        public Uri PictureUrl { get; set; } 
        public bool IsSelected { get; set; }    
    }
   public async Task<IActionResult> Create(PictureModel picture)
        {

            string apiUrl = "https://localhost:6789/api/v1";
            List<ProductViewModel> products = new List<ProductViewModel>();
            HttpClient client = new HttpClient();
            client.BaseAddress = new Uri(apiUrl);
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "ey`enter code here`o");
            HttpResponseMessage response = client.GetAsync(apiUrl + string.Format("/pictureDetails/?studioId={0}&albumid={1}&pageIndex={2}&isTablet={3}&faceid={4}", picture.studioId, picture.albumid, picture.pageIndex, picture.isTablet, picture.faceid)).Result;
            if (response.IsSuccessStatusCode)
            {
                JsonConvert.SerializeObject(picture);
                //var customers =  JsonConvert.DeserializeObject<List<ProductViewModel>>(response.Content.ReadAsStringAsync().Result);
                var apiResponse = await response.Content.ReadAsStringAsync();
                //var  product  JsonConvert.DeserializeObject(apiResponse);
                ProductViewModel p = JsonConvert.DeserializeObject<ProductViewModel>(apiResponse);
                products = JsonConvert.DeserializeObject<List<ProductViewModel>>(response.Content.ReadAsStringAsync().Result);
                return View(products);
            }
            return View();
        }

JSON响应:

{
    "album_id": 99999,
    "album_name": "abcc",
    "is_face_detection": true,
    "face_detection_status_id": 40000,
    "is_face_detection_completed": true,
    "message": "Data fetched successfully",
    "status": true,
    "has_next_page": true,
    "total_images": 1000,
    "selected_images": 0,
    "large_thumb_width": "1600&sharpen=true",
    "picture_list": [
        {
            "PictureId": ,
            "PictureUrl": "‎/152972.jpg?width=775&sharpen=true",
            "IsSelected": false
        },
        {
            "PictureId": 152973,
            "PictureUrl": "‎/152973.jpg?width=775&sharpen=true",
            "IsSelected": false
        },
        {
            "PictureId": 152974,
            "PictureUrl": "‎/152974.jpg?width=775&sharpen=true",
            "IsSelected": false
        },
        {
            "PictureId": 152975,
            "PictureUrl": "‎/152975.jpg?width=775&sharpen=true",
            "IsSelected": false
        },
        {
            "PictureId": 152976,
            "PictureUrl": "‎/152976.jpg?width=775&sharpen=true",
            "IsSelected": false
        },
        {
            "PictureId": 152977,
            "PictureUrl": "‎152977.jpg?width=775&sharpen=true",
            "IsSelected": false
        },
        {
            "PictureId": 152978,
            "PictureUrl": "‎/152978.jpg?width=775&sharpen=true",
            "IsSelected": false
        },
        {
            "PictureId": 152979,
            "PictureUrl": "‎/152979.jpg?width=775&sharpen=true",
            "IsSelected": false
        },
        {
            "PictureId": 152980,
            "PictureUrl": "‎/152980.jpg?width=775&sharpen=true",
            "IsSelected": false
        },
        {
            "PictureId": 152981,
            "PictureUrl": "‎152981.jpg?width=775&sharpen=true",
            "IsSelected": false
        },
        {
            "PictureId": 152982,
            "PictureUrl": "‎/152982.jpg?width=775&sharpen=true",
            "IsSelected": false
        },
        {
            "PictureId": 152983,
            "PictureUrl": "‎/152983.jpg?width=775&sharpen=true",
            "IsSelected": false
        }       
    ]
}
wwwo4jvm

wwwo4jvm1#

我的问题解决了。模型类ProductViewModel和Picture_list中有错误。您必须声明与Json响应相同的数据类型和实体名称。所以我根据Web API响应更改了实体名称和类型。
我分享了我的解决代码:

public class ProductViewModel
    {
        public int album_id { get; set; }
        public string album_name { get; set; }
        public bool is_face_detection { get; set; }
        public int face_detection_status_id { get; set; }
        public bool is_face_detection_completed { get; set; }
        public string message { get; set; }
        public bool status { get; set; }
        public bool has_next_page { get; set; }
        public int total_images { get; set; }
        public int selected_images { get; set; }
        public string large_thumb_width { get; set; }
        public IList<Picture_list> picture_list { get; set; }
    }

public class Picture_list
    {
        public int PictureId { get; set; }
        public string PictureUrl { get; set; }
        public bool IsSelected { get; set; }
    }

感谢您的支持。

相关问题