Reputation: 3
I am trying to Parse a schema and read an element from it however I am getting an error.
This is my code:
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();
}
}
}
And this is the error I am getting:
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?)
I am trying to follow the sample here: http://james.newtonking.com/projects/json/help/#
Samples -> JsonSchema -> Parse Json schema
And here is the start of the schema I am trying to read from:
{
"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
I thought I did pretty much exactly what the sample did. What have I done wrong? I have searched for similar problems but have not found the answer. Also, if you could let me know the correct way of getting the Def index of "Riki's Dagger" from the json schema that would be great.
Upvotes: 0
Views: 861
Reputation: 977
If you want to read an element from JSON.
Generate classes from your JSON using http://json2csharp.com/ or http://jsonclassgenerator.codeplex.com/ (I feel this is better).
Use JSON.net to deserialize your JSON into root class.
var ouput -JsonConvert.Deserialize<your_root_class>(JSONString);
Just read the value you want to.
Upvotes: 1