2013-08-19 1 views
0

스키마를 파싱하고 요소를 읽으려고하는데 오류가 발생합니다. Json.net JsonSchema trouble C#

내 코드입니다 :

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Data; 
using System.IO; 
using Newtonsoft.Json.Schema; 

namespace ConsoleApplication1 
{ 
    class Test 
    { 
     static void Main(string[] args) 
     { 

      string ingameschemaFilePath = @"C:\Users\Andrew\Documents\GitHub\SteamBot\Bin\Debug\ingameschema.txt"; 
      string dota2schemaFilePath = @"C:\Users\Andrew\Documents\GitHub\SteamBot\Bin\Debug\dota2schema.txt"; 
      string schemaFilePath = @"C:\Users\Andrew\Documents\GitHub\SteamBot\Bin\Debug\schema.txt"; 

      JsonSchema dota2schema = JsonSchema.Parse(File.ReadAllText(dota2schemaFilePath)); 

      Console.WriteLine(dota2schema.result.items.name); 

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

} 

그리고 이것은 내가 점점 오전 오류입니다 : 여기 샘플 따르도록 노력하고

Error 2 'Newtonsoft.Json.Schema.JsonSchema' does not contain a definition for 'result' and no extension method 'result' accepting a first argument of type 'Newtonsoft.Json.Schema.JsonSchema' could be found (are you missing a using directive or an assembly reference?) 

: http://james.newtonking.com/projects/json/help/#

샘플 -> JsonSchema -> Parse Json 스키마

그리고 다음은 내가 읽으려는 스키마의 시작입니다.

{ 
    "result": { 
     "status": 1, 
     "items_game_url": "http:\/\/media.steampowered.com\/apps\/570\/scripts\/items\/items_game.d8ab2f9911cea9d7f4bce1add62c7bb83a902322.txt", 
     "qualities": { 
      "normal": 0, 
      "genuine": 1, 
      "vintage": 2, 
      "unusual": 3, 
      "unique": 4, 
      "community": 5, 
      "developer": 6, 
      "selfmade": 7, 
      "customized": 8, 
      "strange": 9, 
      "completed": 10, 
      "haunted": 11, 
      "tournament": 12, 
      "favored": 13 
     }, 
     "originNames": [ 
      { 
       "origin": 0, 
       "name": "Timed Drop" 
      }, 
      { 
       "origin": 1, 
       "name": "Achievement" 
      }, 
      { 
       "origin": 2, 
       "name": "Purchased" 
      }, 
      { 
       "origin": 3, 
       "name": "Traded" 
      }, 
      { 
       "origin": 4, 
       "name": "Crafted" 
      }, 
      { 
       "origin": 5, 
       "name": "Store Promotion" 
      }, 
      { 
       "origin": 6, 
       "name": "Gifted" 
      }, 
      { 
       "origin": 7, 
       "name": "Support Granted" 
      }, 
      { 
       "origin": 8, 
       "name": "Found in Crate" 
      }, 
      { 
       "origin": 9, 
       "name": "Earned" 
      }, 
      { 
       "origin": 10, 
       "name": "Third-Party Promotion" 
      }, 
      { 
       "origin": 11, 
       "name": "Wrapped Gift" 
      }, 
      { 
       "origin": 12, 
       "name": "Halloween Drop" 
      }, 
      { 
       "origin": 13, 
       "name": "Steam Purchase" 
      }, 
      { 
       "origin": 14, 
       "name": "Foreign Item" 
      }, 
      { 
       "origin": 15, 
       "name": "CD Key" 
      }, 
      { 
       "origin": 16, 
       "name": "Collection Reward" 
      }, 
      { 
       "origin": 17, 
       "name": "Preview Item" 
      }, 
      { 
       "origin": 18, 
       "name": "Steam Workshop Contribution" 
      }, 
      { 
       "origin": 19, 
       "name": "Periodic Score Reward" 
      }, 
      { 
       "origin": 20, 
       "name": "Recycling" 
      }, 
      { 
       "origin": 21, 
       "name": "Tournament Drop" 
      }, 
      { 
       "origin": 22, 
       "name": "Passport Reward" 
      }, 
      { 
       "origin": 23, 
       "name": "Tutorial Drop" 
      } 
     ] 
     , 
     "items": [ 
      { 
       "name": "Riki's Dagger", 
       "defindex": 0, 
       "item_class": "dota_item_wearable", 
       "item_type_name": "#DOTA_WearableType_Daggers", 
       "item_name": "#DOTA_Item_Rikis_Dagger", 
       "proper_name": false, 
       "item_quality": 0, 
       "image_inventory": null, 
       "min_ilevel": 1, 
       "max_ilevel": 1, 
       "image_url": "", 
       "image_url_large": "", 
       "capabilities": { 
        "can_craft_mark": true, 
        "can_be_restored": true, 
        "strange_parts": true, 
        "paintable_unusual": true, 
        "autograph": true 

나는 샘플이했던 것과 거의 똑같이 생각했습니다. 나는 무엇을 잘못 했는가? 비슷한 문제를 찾았지만 대답을 찾지 못했습니다. 또한 json 스키마에서 "Riki 's Dagger"의 Def 인덱스를 얻는 올바른 방법을 알려 주면 좋을 것입니다.

답변

1

JSON에서 요소를 읽으려는 경우.

  1. 것은 http://json2csharp.com/ 또는 http://jsonclassgenerator.codeplex.com/ (나는이 더 나은 느낌)을 사용하여 JSON에서 클래스를 생성합니다.

  2. JSON을 사용하여 JSON을 루트 클래스로 역 직렬화합니다.

    var ouput -JsonConvert.Deserialize<your_root_class>(JSONString); 
    
  3. 원하는 값을 읽는 것만으로.