我尝试使用JSON.NET解析一些JSON,但每当我尝试从任何值中获取值时,它都会返回0.我只想从第一组数据中获取值。这是我的代码:
filters.click(function(e){
e.preventDefault();
var f = $(this);
if(f.is('.active')){
// Apply filters only once
return false;
}
filters.removeClass('active');
f.addClass('active');
// Clone the canvas
var clone = originalCanvas.clone();
// Clone the image stored in the canvas as well
clone[0].getContext('2d').drawImage(originalCanvas[0],0,0);
// Add the clone to the page and trigger
// the Caman library on it
photo.find('canvas').remove().end().append(clone);
var effect = $.trim(f[0].id);
Caman(clone[0], function () {
// If such an effect exists, use it:
if( effect in this){
this[effect]();
this.render();
showDownload(clone[0]);
}
else{
hideDownload();
}
});
});
// Use the mousewheel plugin to scroll
// scroll the div more intuitively
filterContainer.find('ul').on('mousewheel',function(e, delta){
this.scrollLeft -= (delta * 50);
e.preventDefault();
});
这是我的JSON对象类:
string text = listBox1.SelectedItem.ToString();
text = text.Substring(text.LastIndexOf(": ") + 2);
string url = "http://www.gw2spidy.com/api/v0.9/json/item-search/" + text + "/1";
var json = new WebClient().DownloadString(url);
Result itemPrices = JsonConvert.DeserializeObject<Result>(json);
int buyPrice = itemPrices.min_sale_unit_price;
int sellPrice = itemPrices.max_offer_unit_price;
sellPriceLabel.Content = "Highest Sell Price: " + sellPrice;
buyPriceLabel.Content = "Lowest Buy Price: " + buyPrice;
以下是我要解析的JSON:
public class Result
{
public int data_id { get; set; }
public string name { get; set; }
public int rarity { get; set; }
public int restriction_level { get; set; }
public string img { get; set; }
public int type_id { get; set; }
public int sub_type_id { get; set; }
public string price_last_changed { get; set; }
public int max_offer_unit_price { get; set; }
public int min_sale_unit_price { get; set; }
public int offer_availability { get; set; }
public int sale_availability { get; set; }
public int sale_price_change_last_hour { get; set; }
public int offer_price_change_last_hour { get; set; }
}
public class RootObject
{
public int count { get; set; }
public int page { get; set; }
public int last_page { get; set; }
public int total { get; set; }
public List<Result> results { get; set; }
}
答案 0 :(得分:1)
刚刚更改了以下代码:
var itemPrices = JsonConvert.DeserializeObject<RootObject>(json);
另外我建议您使用serialization attribute这样的创建属性,而不是像data_id那样:
[DataContract(Name = "result")]
public class Result
{
[DataMamber(Name = "data_id")]
public int Id {get;set;}
.........
}
<强>更新强>
只需确保更改有关返回对象的其余代码。 Desrialize方法返回包含结果列表的RootObejct,因此会影响其余代码
答案 1 :(得分:0)
您必须将json文本解析为RootObject,然后通过results属性访问Result对象。
我已经复制了您的文字,附加了大括号&#34;}&#34;在最后并保存到文件。
一切似乎都有效。
using System;
using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
var json = File.ReadAllText("C:\\Users\\Tynar\\Documents\\json.txt");
RootObject itemPrices = JsonConvert.DeserializeObject<RootObject>(json);
int buyPrice = itemPrices.results[0].min_sale_unit_price;
int sellPrice = itemPrices.results[0].max_offer_unit_price;
Console.WriteLine(buyPrice);
Console.WriteLine(sellPrice);
Console.ReadLine();
}
}
public class Result
{
public int data_id { get; set; }
public string name { get; set; }
public int rarity { get; set; }
public int restriction_level { get; set; }
public string img { get; set; }
public int type_id { get; set; }
public int sub_type_id { get; set; }
public string price_last_changed { get; set; }
public int max_offer_unit_price { get; set; }
public int min_sale_unit_price { get; set; }
public int offer_availability { get; set; }
public int sale_availability { get; set; }
public int sale_price_change_last_hour { get; set; }
public int offer_price_change_last_hour { get; set; }
}
public class RootObject
{
public int count { get; set; }
public int page { get; set; }
public int last_page { get; set; }
public int total { get; set; }
public List<Result> results { get; set; }
}
}