C# Json数组到对象转换器错误
我正在尝试反序列化json,它有时是数组,有时是可手工制作的对象: 不要看这里的代码,我制作了一个可运行的版本,以便更容易理解问题,请向下滚动,不管怎样:- 有时这是一个对象:C# Json数组到对象转换器错误,c#,json,json.net,C#,Json,Json.net,我正在尝试反序列化json,它有时是数组,有时是可手工制作的对象: 不要看这里的代码,我制作了一个可运行的版本,以便更容易理解问题,请向下滚动,不管怎样:- 有时这是一个对象: "prices": { "5": { "Tradable": { "Craftable": { "10": { "currency": "earbuds", "v
"prices": {
"5": {
"Tradable": {
"Craftable": {
"10": {
"currency": "earbuds",
"value": 4.5,
"last_update": 1404866151,
"difference": 76.95,
"value_high": 5
},
"11": {
"currency": "earbuds",
"difference": 7.0965,
"last_update": 1400430312,
"value": 1.8,
"value_high": 2.1
}
有时它是一个带有一个单元格的数组:
"A Distinctive Lack of Hue": {
"defindex": [
5040
],
"prices": {
"6": {
"Tradable": {
"Craftable": [
{
"currency": "keys",
"value": 2,
"last_update": 1393465587,
"difference": 0.35275
}
]
我试着把它推广到这些课程中:
public class Tradable
{
[JsonProperty("Craftable")]
[JsonConverter(typeof(CategoryConverter))]
public IDictionary<string, Craftable> Craftable { get; set; }
}
public class Craftable
{
public IDictionary<string, Priceindex> Priceindex { get; set; }
}
e、 InnerException。消息:
Unable to cast object of type 'Craftable' to type 'System.Collections.Generic.IDictionary`2[System.String,SteamTrade.PriceSchema+Craftable]'.
这是我的转换器:
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
JToken token = JToken.Load(reader);
if (token.Type == JTokenType.Object)
{
return token.ToObject<Craftable>();
}
else if (token.Type == JTokenType.Array)
{
return token.First.ToObject<Craftable>();
}
throw new JsonSerializationException("Unexpected token type: " + token.Type.ToString());
}
编辑:
你可以更容易地理解这个计划
可运行代码:
此处文件结尾^将此公共IDictionary可手工制作{get;set;}修改为可手工制作的公共列表。理想情况下,您可以为每个对象创建自己的类。签出以将json转换为cobjects@dsum27tnx表示响应,但正如您在示例中看到的一样,在同一个可手工制作的字段中,我的问题是转换器由于某种原因而失败,我不理解。我已经新建了一个你想要的网站,但是它在尝试解决json解析时出现了错误,你的json不起作用。请确认它是有效的。已经这么做了吗?请让我知道,这样我就可以修复它。我还将您的json代码粘贴到转换工具中,我不得不做一些修改来修复json,但后来它成功了。出现无效的字段您可以更改名称,并在属性上方添加[JsonPropertyWhat the invalid name is]。@dsum27我真的不明白您的意思,上面是示例,完整的json就是这样,有很多名称我无法全部硬编码,这就是我使用IDictionary的原因。@dsum27 tnx,但是我问了一个不知怎么会失败的转换器,不是关于我的类,我明天会尝试将所有的模块转换成exe,这样会更容易阅读。tnx求救,伙计。
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
JToken token = JToken.Load(reader);
if (token.Type == JTokenType.Object)
{
return token.ToObject<Craftable>();
}
else if (token.Type == JTokenType.Array)
{
return token.First.ToObject<Craftable>();
}
throw new JsonSerializationException("Unexpected token type: " + token.Type.ToString());
}
public class Priceindex
{
[JsonProperty("currency")]
public string currency { get; set; }
[JsonProperty("value")]
public decimal value { get; set; }
[JsonProperty("value_old")]
public Priceindex value_old { get; set; }
[JsonProperty("value_high")]
public string value_high { get; set; }
//[JsonProperty("value_raw")]
//public string currency { get; set; }
[JsonProperty("last_update")]
public int last_update { get; set; }
[JsonProperty("difference")]
public decimal difference { get; set; }
}
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using System.Net;
using System.IO;
using System.Threading;
using SteamTrade;
using Newtonsoft.Json.Linq;
namespace SteamTrade
{
/// <summary>
/// This class represents the TF2 Item prices schema from backpack.tf as deserialized from its
/// JSON representation.
/// </summary>
public class PriceSchema
{
/// <summary>
/// Fetches the Tf2 Item schema.
/// </summary>
/// <param name="apiKey">The API key.</param>
/// <returns>A deserialized instance of the Item Schema.</returns>
/// <remarks>
/// The schema will be cached for future use if it is updated.
/// </remarks>
[STAThread]
public static void Main(string[] args)
{
// Convert the Json
string result = File.ReadAllText("Json.txt");
Data schemaResult = JsonConvert.DeserializeObject<Data>(result);
}
public class Data
{
[JsonProperty("response")]
public Response response { get; set; }
}
public class Response
{
[JsonProperty("success")]
public int success { get; set; }
[JsonProperty("message ")]
public string message { get; set; }
[JsonProperty("current_time")]
public string current_time { get; set; }
[JsonProperty("raw_usd_value")]
public string raw_usd_value { get; set; }
[JsonProperty("usd_currency ")]
public string usd_currency { get; set; }
[JsonProperty("usd_currency_index")]
public int usd_currency_index { get; set; }
[JsonProperty("items")]
public IDictionary<string, ItemName> items { get; set; }
//public Items[] items { get; set; }
}
public class Items
{
public ItemName ItemName { get; set; }
}
public class ItemName
{
[JsonProperty("defindex")]
public int[] Defindex { get; set; }
[JsonProperty("prices")]
public IDictionary<string, QualityInteger> Prices { get; set; }
}
public class Prices
{
public QualityInteger QualityInteger { get; set; }
}
public class QualityInteger
{
public Tradable Tradable { get; set; }
public Untradable Untradable { get; set; }
}
public class Tradable
{
[JsonProperty("Craftable")]
[JsonConverter(typeof(CategoryConverter))]
public IDictionary<string, Craftable> Craftable { get; set; }
//public List<Craftable> craftable { get; set; }
[JsonProperty("Non-Craftable")]
public List<Uncraftable> Uncraftable { get; set; }
}
public class Untradable
{
[JsonProperty("Craftable")]
[JsonConverter(typeof(CategoryConverter))]
public Craftable Craftable { get; set; }
//public IDictionary<string, Craftable> Craftable { get; set; }
//public List<Craftable> craftable { get; set; }
[JsonProperty("Non-Craftable")]
public List<Uncraftable> Uncraftable { get; set; }
}
#region Overrdie Reader
class CategoryConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return (objectType == typeof(string[]));
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
JToken token = JToken.Load(reader);
if (token.Type == JTokenType.Object)
{
return token.ToObject<Craftable>();
}
else if (token.Type == JTokenType.Array)
{
return token.First.ToObject<Craftable>();
}
throw new JsonSerializationException("Unexpected token type: " + token.Type.ToString());
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
#endregion
public class Craftable
{
//public List<Priceindex> Priceindex { get; set; }
public IDictionary<string, Priceindex> Priceindex { get; set; }
//public Priceindex Priceindex { get; set; }
}
public class Uncraftable
{
//public Priceindex Priceindex { get; set; }
public IDictionary<string, Priceindex> Priceindex { get; set; }
//public Priceindex Priceindex { get; set; }
}
public class Priceindex
{
[JsonProperty("currency")]
public string currency { get; set; }
[JsonProperty("value")]
public decimal value { get; set; }
[JsonProperty("value_old")]
public Priceindex value_old { get; set; }
[JsonProperty("value_high")]
public string value_high { get; set; }
//[JsonProperty("value_raw")]
//public string currency { get; set; }
[JsonProperty("last_update")]
public int last_update { get; set; }
[JsonProperty("difference")]
public decimal difference { get; set; }
}
}
}
{
"response": {
"success": 1,
"current_time": 1405635143,
"raw_usd_value": 0.245,
"usd_currency": "metal",
"usd_currency_index": 5002,
"items": {
"A Brush with Death": {
"defindex": [
30186
],
"prices": {
"6": {
"Tradable": {
"Craftable": [
{
"currency": "metal",
"value": 3.66,
"last_update": 1405098842,
"difference": -0.34
}
]
}
}
}
},
"A Rather Festive Tree": {
"defindex": [
341
],
"prices": {
"5": {
"Tradable": {
"Craftable": {
"10": {
"currency": "earbuds",
"value": 4.5,
"last_update": 1404866151,
"difference": 76.95,
"value_high": 5
},
"11": {
"currency": "earbuds",
"difference": 7.0965,
"last_update": 1400430312,
"value": 1.8,
"value_high": 2.1
},
}
}
"6": {
"Tradable": {
"Craftable": [
{
"currency": "metal",
"value": 1.66,
"last_update": 1400374538,
"difference": -0.17
}
],
"Non-Craftable": [
{
"currency": "metal",
"value": 1.33,
"last_update": 1400368707,
"difference": -0.22
}
]
}
}
}
},
}
}
}