-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
63 lines (54 loc) · 2.24 KB
/
Program.cs
File metadata and controls
63 lines (54 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Encodings.Web;
namespace JSONParserVoorbeeld
{
class Program
{
static void Main(string[] args)
{
/**
* Tip:
* Open de properties van TempleOfDoom.json.
* Zie dat build action op None staat -> Hij komt niet in je .exe / .dll file
* Zie dat de copy to output directory op 'Copy if newer' staat ->
* Hij is dus altijd relatief te benaderen om dat hij na het builden ook bij je .exe / .dll staat.
*
*/
string fileName = "./Files/TempleOfDoom.json";
/**
* We gebruiken de nuget package Newtonsoft.json
* Kijk voor documentatie op: http://www.newtonsoft.com/json/help
*/
JObject json = JObject.Parse(File.ReadAllText(fileName));
// Ga key value based door je json heen.
JToken jtoken = json["rooms"][2]["items"][0]["type"];
string type = jtoken.Value<string>();
Console.WriteLine(type);
// Parsing kan met generics naar het juiste type.
jtoken = json.SelectToken("$.rooms[2].items[0].damage");
int damage = jtoken.Value<int>();
Console.WriteLine(damage);
// Of loop door je children:
foreach (JObject jconnection in json["connections"])
{
foreach (JProperty jProperty in jconnection.Children().OfType<JProperty>())
{
Console.WriteLine($"Key: {jProperty.Name}, Value: {jProperty.Value}");
}
}
// Of krijg een key/value dictionary van je items:
JToken jFirstRoom = json["rooms"][0];
Dictionary<string, string> roomProperties = jFirstRoom.ToObject<Dictionary<string, string>>();
Console.WriteLine(roomProperties["width"]);
// Of je parst direct naar je (parsed) objecten:
JToken jPlayer= json["player"];
ParsedPlayer parsedPlayer = jPlayer.ToObject<ParsedPlayer>();
Console.WriteLine(parsedPlayer);
Console.ReadKey();
}
}
}