尝试使用JSON.NET在C#中解析JSON

  • 本文关键字:JSON NET c# json json.net
  • 更新时间 :
  • 英文 :


我正在尝试使用json.net解析一些JSON,但是每当我尝试从任何值中获取值时,它都会返回0。我只想从第一组数据。这是我的代码:

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; }
}

这是我要解析的JSON:

{
"count": 3,
"page": 1,
"last_page": 1,
"total": 3,
"results": [
    {
        "data_id": 12223,
        "name": "Apple Pie",
        "rarity": 2,
        "restriction_level": 10,
        "img": "https://render.guildwars2.com/file/0A50099C343F01AC2846ADF4C8A948BA76F4DBC1/63097.png",
        "type_id": 3,
        "sub_type_id": 1,
        "price_last_changed": "2015-05-05 20:58:24 UTC",
        "max_offer_unit_price": 136,
        "min_sale_unit_price": 226,
        "offer_availability": 22161,
        "sale_availability": 4007,
        "sale_price_change_last_hour": 0,
        "offer_price_change_last_hour": 0
    },
    {
        "data_id": 12150,
        "name": "Eda's Apple Pie",
        "rarity": 1,
        "restriction_level": 5,
        "img": "https://render.guildwars2.com/file/13380176D1D569B5DD077F7DD8C412CAE9E77527/63254.png",
        "type_id": 3,
        "sub_type_id": 1,
        "price_last_changed": "2015-05-05 23:31:06 UTC",
        "max_offer_unit_price": 160,
        "min_sale_unit_price": 313,
        "offer_availability": 3596,
        "sale_availability": 2744,
        "sale_price_change_last_hour": 0,
        "offer_price_change_last_hour": 0
    },
    {
        "data_id": 9497,
        "name": "Eda's Apple Pie Recipe",
        "rarity": 1,
        "restriction_level": 0,
        "img": "https://render.guildwars2.com/file/B7B167286DD34B254E22682900C6EF2310F6EE0E/849342.png",
        "type_id": 3,
        "sub_type_id": 6,
        "price_last_changed": "2014-09-11 10:12:00 UTC",
        "max_offer_unit_price": 10101,
        "min_sale_unit_price": 0,
        "offer_availability": 0,
        "sale_availability": 0,
        "sale_price_change_last_hour": 0,
        "offer_price_change_last_hour": 0
    }
]}

刚刚更改了以下代码:

var itemPrices = JsonConvert.DeserializeObject<RootObject>(json);

我也建议您使用像data_id这样的属性,您可以使用这样的序列化属性:

[DataContract(Name = "result")]
public class Result
{
   [DataMamber(Name = "data_id")]
   public int Id {get;set;}
   .........
}

更新

只需确保更改有关返回对象的其余代码即可。desrialize方法返回rootobejct,其中包含结果列表,因此将对您的其余代码产生影响

您必须将JSON文本解析为rootObject,然后通过结果属性访问结果对象。

我已经复制了您的文本,最后附加了闭合括号"}",并保存到文件中。

一切似乎都起作用。

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; }
    }
}

相关内容

  • 没有找到相关文章

最新更新