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
I was able to do the search with your JSON file using the native JavaScriptSerializer.
I changed the resulting object to
And changed conversion code to