繁体   English   中英

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

[英]Trying to Parse JSON in C# using 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,然后通过results属性访问Result对象。

我已经复制了您的文本,并在末尾附加了右大括号“}”并保存到文件中。

一切似乎都正常。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM