0

所以我正在尝试使用 defindex 搜索我的 json 文件中的项目并返回项目名称,但是我得到一个错误。我进行了搜索,但似乎没有什么相关的。

            var json = File.ReadAllText(dota2schemaFilePath);
            var dota2schema = JsonConvert.DeserializeObject<schema>(json);

            foreach (Item item in Items)     //ERROR HERE
            {
                if (item.Defindex == 4793)
                    itemname = item.Name;

错误 2 非静态字段、方法或属性“schemaexample.schema.Items.get”需要对象引用

它似乎需要一个参考。我如何简单地搜索项目?

这是我的完整代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.IO;
using System.Threading;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;

namespace schemaexample
{

    public class schema
    {
        [JsonProperty("items")]
        public Item[] Items { get; set; }

        [JsonProperty("items_game_url")]
        public string ItemsGameUrl { get; set; }

        [JsonProperty("status")]
        public int Status { get; set; }

        public class Item
        {
            [JsonProperty("name")]
            public string Name { get; set; }

            [JsonProperty("defindex")]
            public int Defindex { get; set; }

            [JsonProperty("item_class")]
            public string ItemClass { get; set; }

            [JsonProperty("item_type_name")]
            public string ItemTypeName { get; set; }

            [JsonProperty("item_name")]
            public string ItemName { get; set; }

            [JsonProperty("proper_name")]
            public bool ProperName { get; set; }

            [JsonProperty("item_quality")]
            public int ItemQuality { get; set; }

            [JsonProperty("image_inventory")]
            public string ImageInventory { get; set; }

            [JsonProperty("min_ilevel")]
            public int MinIlevel { get; set; }

            [JsonProperty("max_ilevel")]
            public int MaxIlevel { get; set; }

            [JsonProperty("image_url")]
            public string ImageUrl { get; set; }

            [JsonProperty("image_url_large")]
            public string ImageUrlLarge { get; set; }

            [JsonProperty("item_description")]
            public string ItemDescription { get; set; }

            [JsonProperty("attributes")]
            public Attribute[] Attributes { get; set; }

            [JsonProperty("item_set")]
            public string ItemSet { get; set; }
        }

        public static void Main(string[] args)
        {
            string dota2schemaFilePath = @"C:\Users\Andrew\Documents\GitHub\SteamBot\Bin\Debug\dota2schema.txt";
            string itemname = "not set";

            var json = File.ReadAllText(dota2schemaFilePath);
            var dota2schema = JsonConvert.DeserializeObject<schema>(json);

            foreach (Item item in Items)     //ERROR HERE
            {
                if (item.Defindex == 4793)
                    itemname = item.Name;
            }
            Console.WriteLine(itemname);

            Console.WriteLine("Press any key to exit");
            Console.ReadKey();


        }

        protected class schemaresult
        {
            public schema result { get; set; }
        }

    }
}

如果你需要它,我正在阅读的 json 在这里: http ://www50.zippyshare.com/v/13310944/file.html

4

1 回答 1

0

我能够使用本机 JavaScriptSerializer 对您的 JSON 文件进行搜索。

我将结果对象更改为

public class schemaresult
{
    public schema result { get; set; }
}

public class schema
{
    public Item[] Items { get; set; }
    public string ItemsGameUrl { get; set; }
    public int Status { get; set; }
}

public class Item
{
    public string Name { get; set; }
    public int Defindex { get; set; }
    public string ItemClass { get; set; }
    public string ItemTypeName { get; set; }
    public string ItemName { get; set; }
    public bool ProperName { get; set; }
    public int ItemQuality { get; set; }
    public string ImageInventory { get; set; }
    public int MinIlevel { get; set; }
    public int MaxIlevel { get; set; }
    public string ImageUrl { get; set; }
    public string ImageUrlLarge { get; set; }
    public string ItemDescription { get; set; }
    public Attribute[] Attributes { get; set; }
    public string ItemSet { get; set; }
}
public class Attribute {
    public string name { get; set; }
    public string @class { get; set; }
    public string value { get; set; }
}

并将转换代码更改为

var json = File.ReadAllText(dota2schemaFilePath);
        System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
        serializer.MaxJsonLength = Int32.MaxValue;
        var schema_result = serializer.Deserialize<schemaresult>(json);

        foreach (Item item in schema_result.result.Items )      
        {
            if (item.Defindex == 4793)
            {
                itemname = item.Name;
                break;
            }
        };
于 2013-08-31T13:21:54.453 回答