0

我正在尝试解析模式并从中读取元素,但是出现错误。

这是我的代码:

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 -> 解析 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 索引的正确方法,那就太好了。

4

1 回答 1

1

如果你想从 JSON 中读取一个元素。

  1. 使用http://json2csharp.com/http://jsonclassgenerator.codeplex.com/从您的 JSON 生成类(我觉得这样更好)。

  2. 使用 JSON.net 将您的 JSON 反序列化为根类。

    var ouput -JsonConvert.Deserialize<your_root_class>(JSONString);
    
  3. 只需读取您想要的值。

于 2013-08-19T10:22:53.827 回答