C# 使用多个对象解析Json newtonsoft
我试图使用C#中的newtonsoft解析一个相当复杂/不必要的JSON输出,但是出于某种原因,我的解析器总是返回null,并且没有详细说明为什么会出现这种情况 我试图解析的JSON文件示例如下:C# 使用多个对象解析Json newtonsoft,c#,json,json.net,C#,Json,Json.net,我试图使用C#中的newtonsoft解析一个相当复杂/不必要的JSON输出,但是出于某种原因,我的解析器总是返回null,并且没有详细说明为什么会出现这种情况 我试图解析的JSON文件示例如下: { "response": { "success": 1, "current_time": 1362339098, "prices": { "35": { "11": {
{
"response": {
"success": 1,
"current_time": 1362339098,
"prices": {
"35": {
"11": {
"0": {
"current": {
"currency": "keys",
"value": 39,
"value_high": 41,
"date": 1357515306
},
"previous": {
"currency": "keys",
"value": 37,
"value_high": 39
}
}
},
"3": {
"0": {
"current": {
"currency": "metal",
"value": 0.33,
"value_high": 0.66
}
}
}
},
"5002": {
"6": {
"0": {
"current": {
"currency": "usd",
"value": 0.39,
"value_high": 0.42,
"date": 1358090106
}
}
}
},
"5022": {
"6": {
"1": {
"current": {
"currency": "metal",
"value": 1.33,
"value_high": 1.55,
"date": 1357515175
}
}
}
}
}
}
}
还有我正在使用的C#解析器。我运行getCurrentPrices()返回PriceParser对象,但返回的对象始终为null
using System;
using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json;
using System.Diagnostics;
namespace SteamBot
{
class PriceParser
{
//Methods
public PriceParser updatePrices()
{
var json = File.ReadAllText("test.json");
ParserResult result = JsonConvert.DeserializeObject<ParserResult>(json);
return result.result;
}
public Data currentPrices { get; set; }
//DATA
public class Data
{
public Response Response { get; set; }
}
public class Response
{
public string success { get; set; }
public string current_time {get; set;}
public List<Price> prices { get; set;}
}
public class Price
{
public int defindex { get; set; }
public int quality { get; set; }
public Current current { get; set; }
public Previous previous { get; set; }
}
public class Current
{
public string currency { get; set; }
public float value { get; set; }
public float value_high { get; set; }
public int date { get; set; }
}
public class Previous
{
public string currency { get; set; }
public float value { get; set; }
public float value_high { get; set; }
public int date { get; set; }
}
protected class ParserResult
{
public PriceParser result { get; set; }
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.IO;
使用Newtonsoft.Json;
使用系统诊断;
命名空间蒸汽机器人
{
类价格分析器
{
//方法
公共价格分析器更新价格()
{
var json=File.ReadAllText(“test.json”);
ParserResult result=JsonConvert.DeserializeObject(json);
返回result.result;
}
公共数据当前价格{get;set;}
//资料
公共类数据
{
公共响应{get;set;}
}
公众课堂反应
{
公共字符串成功{get;set;}
公共字符串当前_时间{get;set;}
公开标价{get;set;}
}
公共类价格
{
公共int defindex{get;set;}
公共整数质量{get;set;}
公共当前{get;set;}
公共上一个{get;set;}
}
公共类电流
{
公共字符串货币{get;set;}
公共浮点值{get;set;}
公共浮点值_high{get;set;}
公共整数日期{get;set;}
}
公共课前
{
公共字符串货币{get;set;}
公共浮点值{get;set;}
公共浮点值_high{get;set;}
公共整数日期{get;set;}
}
受保护类ParserResult
{
公共价格分析器结果{get;set;}
}
}
}
我可能只是错过了一些愚蠢的事情,但就我的一生而言,我不知道是什么,任何有更多JSON争论经验的人都知道这里发生了什么?你得到的是空值,因为你的类结构与你的JSON不匹配 第一个问题是,当您应该使用
数据时,您正在反序列化为ParserResult
<代码>数据
有一个与JSON匹配的响应
属性ParserResult
没有此属性
第二个问题是,您已经将价格
定义为列表
,但您的JSON不包含数组。相反,JSON结构实际上是一系列嵌套字典
请尝试像这样定义内部类:
public class Data
{
public Response response { get; set; }
}
public class Response
{
public int success { get; set; }
public long current_time { get; set; }
public IDictionary<int, IDictionary<int, IDictionary<int, Price>>> prices { get; set; }
}
public class Price
{
public Quote current { get; set; }
public Quote previous { get; set; }
}
public class Quote
{
public string currency { get; set; }
public decimal value { get; set; }
public decimal value_high { get; set; }
public long date { get; set; }
}
public PriceParser updatePrices()
{
var json = File.ReadAllText("test.json");
currentPrices = JsonConvert.DeserializeObject<Data>(json);
return this;
}
以下是您将如何转储数据:
PriceParser parser = new PriceParser();
parser.updatePrices();
foreach (var defindex in parser.currentPrices.response.prices)
{
Console.WriteLine("defindex: " + defindex.Key);
foreach (var quality in defindex.Value)
{
Console.WriteLine("\t quality: " + quality.Key);
foreach (var price in quality.Value)
{
Console.WriteLine("\t\t index: " + price.Key);
Console.WriteLine("\t\t\t current price:");
Console.WriteLine("\t\t\t\t currency: " + price.Value.current.currency);
Console.WriteLine("\t\t\t\t value: " + price.Value.current.value);
Console.WriteLine("\t\t\t\t value_high: " + price.Value.current.value_high);
if (price.Value.previous != null)
{
Console.WriteLine();
Console.WriteLine("\t\t\t previous price:");
Console.WriteLine("\t\t\t\t currency: " + price.Value.previous.currency);
Console.WriteLine("\t\t\t\t value: " + price.Value.previous.value);
Console.WriteLine("\t\t\t\t value_high: " + price.Value.previous.value_high);
}
}
}
}
这是上面的输出:
defindex: 35
quality: 3
index: 0
current price:
currency: metal
value: 0.33
value_high: 0.66
quality: 11
index: 0
current price:
currency: keys
value: 39
value_high: 41
previous price:
currency: keys
value: 37
value_high: 39
defindex: 5002
quality: 6
index: 0
current price:
currency: usd
value: 0.39
value_high: 0.42
defindex: 5022
quality: 6
index: 1
current price:
currency: metal
value: 1.33
value_high: 1.55
由于类结构与JSON不匹配,因此将获得空值 第一个问题是,当您应该使用
数据时,您正在反序列化为ParserResult
<代码>数据
有一个与JSON匹配的响应
属性ParserResult
没有此属性
第二个问题是,您已经将价格
定义为列表
,但您的JSON不包含数组。相反,JSON结构实际上是一系列嵌套字典
请尝试像这样定义内部类:
public class Data
{
public Response response { get; set; }
}
public class Response
{
public int success { get; set; }
public long current_time { get; set; }
public IDictionary<int, IDictionary<int, IDictionary<int, Price>>> prices { get; set; }
}
public class Price
{
public Quote current { get; set; }
public Quote previous { get; set; }
}
public class Quote
{
public string currency { get; set; }
public decimal value { get; set; }
public decimal value_high { get; set; }
public long date { get; set; }
}
public PriceParser updatePrices()
{
var json = File.ReadAllText("test.json");
currentPrices = JsonConvert.DeserializeObject<Data>(json);
return this;
}
以下是您将如何转储数据:
PriceParser parser = new PriceParser();
parser.updatePrices();
foreach (var defindex in parser.currentPrices.response.prices)
{
Console.WriteLine("defindex: " + defindex.Key);
foreach (var quality in defindex.Value)
{
Console.WriteLine("\t quality: " + quality.Key);
foreach (var price in quality.Value)
{
Console.WriteLine("\t\t index: " + price.Key);
Console.WriteLine("\t\t\t current price:");
Console.WriteLine("\t\t\t\t currency: " + price.Value.current.currency);
Console.WriteLine("\t\t\t\t value: " + price.Value.current.value);
Console.WriteLine("\t\t\t\t value_high: " + price.Value.current.value_high);
if (price.Value.previous != null)
{
Console.WriteLine();
Console.WriteLine("\t\t\t previous price:");
Console.WriteLine("\t\t\t\t currency: " + price.Value.previous.currency);
Console.WriteLine("\t\t\t\t value: " + price.Value.previous.value);
Console.WriteLine("\t\t\t\t value_high: " + price.Value.previous.value_high);
}
}
}
}
这是上面的输出:
defindex: 35
quality: 3
index: 0
current price:
currency: metal
value: 0.33
value_high: 0.66
quality: 11
index: 0
current price:
currency: keys
value: 39
value_high: 41
previous price:
currency: keys
value: 37
value_high: 39
defindex: 5002
quality: 6
index: 0
current price:
currency: usd
value: 0.39
value_high: 0.42
defindex: 5022
quality: 6
index: 1
current price:
currency: metal
value: 1.33
value_high: 1.55
我已经编辑了你的标题。请看“”,其中的共识是“不,他们不应该”。我已经编辑了你的标题。请看“”,其中的共识是“不,他们不应该”。这非常有效,嵌套字典让我头晕目眩,但我想JSON的输出方式与其说是我所能控制的任何东西,不如说是一个函数。是的,对于API来说,这确实是一种奇怪的格式。根据您的需要,您可以在反序列化后进行一些后处理,将其扁平化为更易于消化的形式。这非常有效,嵌套字典让我头晕目眩,但我猜JSON的输出方式与其说是我可以控制的功能,不如说是我可以控制的功能。是的,对于API来说,这确实是一种奇怪的格式。根据您的需要,您可以在反序列化后进行一些后处理,将其扁平化为更易于消化的形式。