using Newtonsoft.Json; using System; using System.Collections; using System.Collections.Generic; using System.Threading.Tasks; using UnityEngine; public class productInfo22 : MonoBehaviour { //public async Task getMallList(int productType, int pageNo, int pageSize) public async Task productInfo(int productId) { // 准备请求的头部信息,包含授权令牌 Dictionary head22 = new Dictionary { { "Authorization", Global.global.serverResponse.data.token } }; // 请求体 getProdectBody getProdectBody = new getProdectBody { productId = productId, }; Debug.Log("====入参====" + JsonConvert.SerializeObject(getProdectBody)); // 异步发送请求 string response22 = await web.SendRequest(web.URL + "/snail/product/page", "POST", JsonConvert.SerializeObject(getProdectBody), head22); // 调试输出接收到的响应 Debug.Log("2.1商城列表:=============== " + response22); getProductResponse getProductResponse1 = new getProductResponse(); try { // 将 JSON 字符串反序列化为 MallResponse 对象 getProductResponse1 = JsonConvert.DeserializeObject(response22); // 判空:首先检查 getProductResponse 和 getProductResponse.data 是否为 null if (getProductResponse1 != null && getProductResponse1.Data != null && getProductResponse1.Data.DataList != null) { Debug.Log($"Total Products: {getProductResponse1.Data.DataList.Count}"); // 遍历商品列表 foreach (var product in getProductResponse1.Data.DataList) { // 这里输出每个商品的信息 Debug.Log($"Product ID: {product.ProductId}"); Debug.Log($"Product Name: {product.ProductName}"); Debug.Log($"Product Price: {product.Price}"); Debug.Log($"Product Pic: {product.ProductPic}"); Debug.Log($"Product Cover: {product.ProductCover}"); } } else { Debug.LogWarning("响应数据为空或格式错误"); } } catch (Exception ex) { Debug.LogError("反序列化响应失败: " + ex.Message); } return getProductResponse1; } }