【发布时间】:2016-08-31 15:56:27
【问题描述】:
我正在尝试解析一个包含大量对象的 Json。
.json 看起来像这样: :
{
"status" : "success",
"prices" : [
{
"market_hash_name" : "4X4 Car",
"price" : "7.87",
"created_at" : 1472587613
},
{
"market_hash_name" : "Yellow Car",
"price" : "27.75",
"created_at" : 1472519899
}
[...] 等
我只想获取特定市场哈希名称的价格。我该怎么做?
我有这个自动取款机
using System.IO;
using Newtonsoft.Json;
public class MarketHandler
{
//Methods
public MarketHandler updatePrices()
{
var json = File.ReadAllText("PriceSkins.json");
currentPrices = JsonConvert.DeserializeObject<Data>(json);
return this;
}
public Data currentPrices { get; set; }
public class Data
{
public Response response { get; set; }
}
public class Response
{
public string status { get; set; }
public Price prices { get; set; }
}
public class Price
{
public string market_hash_name { get; set; }
public string price { get; set; }
public int created_at { get; set; }
}
【问题讨论】:
-
那么问题出在哪里?另外,我没有看到您实际尝试获取值的代码。
-
您的模型目前与您的 JSON 不匹配 - 您没有 single 价格,您有一个 array 价格。 (我敦促您遵循 .NET 命名约定并使用属性来指定 JSON 表示,顺便说一句。)