Json C# 中的反序列化器从 Json 请求响应返回不正确的值
Json Deserializer in C# is returning Incorrect Values from Json Request Response
我正在尝试 return 从以下 https://www.supremenewyork.com/mobile_stock.json
反序列化的 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);
}
}
}
当我尝试 return 一个值时(例如:responseObject.id)它只会 return '0'
当我尝试 return 完整响应时,它 returns "SupremeMobileMonitor.Program+ProductsAndCategory"
知道为什么我无法得到它 returned 以及我搞砸了我的反序列化吗?不幸的是,端点上称为 "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;
jslowik 对你的问题有正确的答案,如何反序列化它。
要打印对象,您可以创建覆盖 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]
我正在尝试 return 从以下 https://www.supremenewyork.com/mobile_stock.json
反序列化的 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);
}
}
}
当我尝试 return 一个值时(例如:responseObject.id)它只会 return '0'
当我尝试 return 完整响应时,它 returns "SupremeMobileMonitor.Program+ProductsAndCategory"
知道为什么我无法得到它 returned 以及我搞砸了我的反序列化吗?不幸的是,端点上称为 "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;
jslowik 对你的问题有正确的答案,如何反序列化它。
要打印对象,您可以创建覆盖 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]