C#中的Json反序列化程序从Json请求响应返回不正确的值

C#中的Json反序列化程序从Json请求响应返回不正确的值,c#,json,json.net,deserialization,json-deserialization,C#,Json,Json.net,Deserialization,Json Deserialization,我正试图返回从以下位置反序列化的Json响应 下面是我的代码: using System; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Text; using System.Drawing; using System.IO; using System.Net.Http; using Newtonsoft.Json; using Newtonsoft.Json.C

我正试图返回从以下位置反序列化的Json响应

下面是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Text;
using System.Drawing;
using System.IO;
using System.Net.Http;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using RestSharp;
using System.Threading.Tasks;
using System.Globalization;

namespace SupremeMobileMonitor
{
    class Program
    {
        static async Task Main(string[] args)
        {
            var program = new Program();
            await program.GetItem();

        }
        // Declaring variables in the list
        static List<ItemDetail> ProductList = new List<ItemDetail>();
        List<string> productDesc = new List<string> { "new_item", "price", "category", "imageurl", "itemURL" };
        List<string> category = new List<string> { "jackets", "shirts", "tops_sweaters", "pants", "hats", "accessories", "shoes", "skate" };

        //creating a class for intializing Json Deserializer 

        public class MobileStockResponse
    {
        [JsonProperty("unique_image_url_prefixes")]
        public List<object> UniqueImageUrlPrefixes { get; set; }

        [JsonProperty("products_and_categories")]
        public Dictionary<string, List<ProductsAndCategory>> ProductsAndCategories { get; set; }

        [JsonProperty("release_date")]
        public string ReleaseDate { get; set; }

        [JsonProperty("release_week")]
        public string ReleaseWeek { get; set; }
    }
        public partial class ProductsAndCategory
        {
            [JsonProperty("name")]
            public string Name { get; set; }
            [JsonProperty("id")]
            public long Id { get; set; }
            [JsonProperty("image_url")]
            public string ImageUrl { get; set; }
            [JsonProperty("image_url_hi")]
            public string ImageUrlHi { get; set; }
            [JsonProperty("price")]
            public long Price { get; set; }
            [JsonProperty("sale_price")]
            public long SalePrice { get; set; }
            [JsonProperty("new_item")]
            public bool NewItem { get; set; }
            [JsonProperty("position")]
            public long Position { get; set; }
            [JsonProperty("category_name")]
            public string CategoryName { get; set; }


        }


        //Initializing HttpClient for Requests and po
        public async Task GetItem()
        {
            var client = new HttpClient();
            var request = new HttpRequestMessage
            {
                RequestUri = new Uri("https://www.supremenewyork.com/mobile_stock.json"),
                Method = HttpMethod.Get
            };
            var response = await client.SendAsync(request);
            var responseContent = await response.Content.ReadAsStringAsync();
            var responseObject = JsonConvert.DeserializeObject<ProductsAndCategory>(responseContent);

            Console.WriteLine(responseObject);




        }



    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统线程;
使用系统文本;
使用系统图;
使用System.IO;
使用System.Net.Http;
使用Newtonsoft.Json;
使用Newtonsoft.Json.Converters;
使用RestSharp;
使用System.Threading.Tasks;
利用制度全球化;
名称空间超级监视器
{
班级计划
{
静态异步任务主(字符串[]args)
{
var program=新程序();
等待程序.GetItem();
}
//在列表中声明变量
静态列表ProductList=新列表();
List productDesc=新列表{“新项目”、“价格”、“类别”、“图像URL”、“项目URL”};
列表类别=新列表{“夹克”、“衬衫”、“上衣”、“毛衣”、“裤子”、“帽子”、“配饰”、“鞋子”、“滑板”};
//创建用于初始化Json反序列化器的类
公共类MobileStockResponse
{
[JsonProperty(“唯一的\u图像\u url\u前缀”)]
公共列表UniqueImageUrlPrefixes{get;set;}
[JsonProperty(“产品和类别”)]
公共字典ProductsAndCategories{get;set;}
[JsonProperty(“发布日期”)]
公共字符串ReleaseDate{get;set;}
[JsonProperty(“发布周”)]
公共字符串ReleaseWeek{get;set;}
}
公共部分类ProductsAndCategory
{
[JsonProperty(“名称”)]
公共字符串名称{get;set;}
[JsonProperty(“id”)]
公共长Id{get;set;}
[JsonProperty(“图像url”)]
公共字符串ImageUrl{get;set;}
[JsonProperty(“image\u url\u hi”)]
公共字符串ImageUrlHi{get;set;}
[JsonProperty(“价格”)]
公共多头价格{get;set;}
[JsonProperty(“销售价格”)]
公共长期销售价格{get;set;}
[JsonProperty(“新项目”)]
公共bool NewItem{get;set;}
[JsonProperty(“职位”)]
公共长位置{get;set;}
[JsonProperty(“类别名称”)]
公共字符串CategoryName{get;set;}
}
//为请求和采购订单初始化HttpClient
公共异步任务GetItem()
{
var client=新的HttpClient();
var请求=新的HttpRequestMessage
{
RequestUri=新的Uri(“https://www.supremenewyork.com/mobile_stock.json"),
Method=HttpMethod.Get
};
var response=wait client.sendaync(请求);
var responseContent=await response.Content.ReadAsStringAsync();
var responseObject=JsonConvert.DeserializeObject(responseContent);
Console.WriteLine(响应对象);
}
}
}
当我尝试返回一个值(例如:responseObject.id)时,它只会返回“0”

当我尝试返回完整的响应时,它返回“supremomobilemonitor.Program+ProductsAndCategory”


你知道为什么我不能把它拿回来,我搞砸了我的反序列化吗?不幸的是,端点上称为“new”的类别干扰了C#关键字,因此删除了使用动态反序列化的选项。

我会立即反序列化整个对象。例如:

// Full model represented by your class
var responseObject = JsonConvert.DeserializeObject<MobileStockResponse>(responseContent);

// The products and categories dictionaries you're looking for
var productsAndCategories = responseObject.ProductsAndCategories;
//由类表示的完整模型
var responseObject=JsonConvert.DeserializeObject(responseContent);
//您正在查找的产品和类别词典
var productsAndCategories=responseObject.productsAndCategories;

jslowik为您的问题提供了正确答案,即如何对其进行反序列化

要打印对象,您可以创建一个override
ToString()
方法并只打印您感兴趣的内容,也可以通过将对象序列化为字符串来打印对象

Console.WriteLine(JsonConvert.SerializeObject(productsAndCategories, Formatting.Indented);
这将再次为您提供对象的json表示,并显示所有值

旁注:如果你想从(比如)包中获取特定的物品,你可以使用Linq来获取

Console.WriteLine(JsonConvert.SerializeObject(responseObject.ProductsAndCategories["Bags"].Select(x => x.Id)));

// Output:
[173389,172978,173019,172974,173018,173001]

使用
Console.WriteLine(prodcuts和categories)时它返回以下内容:
System.Collections.Generic.Dictionary`2[System.String,System.Collections.Generic.List`1[supremobilemonitor.Program+productsandcography]
您不能真正查看这样的对象。设置一个断点并使用调试器,您应该可以看到您要查找的内容。此外,您不需要将产品和类别设置为离散变量。是的,这个类是基于我从API中提取的json创建的。或者你可以序列化刚刚反序列化的对象。那也行:-D。。。。贾瓦德比我快。