Question

So i'm trying to search through the items in my json file using a defindex and return the item name however I get an error. I searched but nothing seemed relevant.

            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;

Error 2 An object reference is required for the non-static field, method, or property 'schemaexample.schema.Items.get'

It seems to wants a reference. How do I simply search through the items?

Here is my full code:

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

    }
}

If you need it the json I am reading is here: http://www50.zippyshare.com/v/13310944/file.html

Était-ce utile?

La solution

I was able to do the search with your JSON file using the native JavaScriptSerializer.

I changed the resulting object to

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

And changed conversion code to

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;
            }
        };
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top