2013-08-31 51 views
0

因此,我想通过使用defindex在我的json文件中搜索物品并返回物品名称,但是出现错误。我搜查了但没有任何关系。通过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

回答

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