The first issue is that your JSON is invalid - you have commas in wrong places. This could be because you trimmed the data. You chan check this here:
JSON Online Validator and Formatter - JSON Lint[
^]. The error message it gives is:
Error: Parse error on line 69:
...": null }, ] }, "currency
---------------------^
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '[', got ']'
Here is the corrected JSON data:
{
"gameConfigurationsReturnCount": 1,
"gameConfigurations": [
{
"gameId": 4,
"gameConfiguration": {
"defaultGameSettings": {
"gameId": 4,
"gId": "wm",
"name": "mel",
"gameType": "slot",
"description": "none",
"refundInactiveBets": true,
"supportedDenominations": [
0.01,
0.02,
0.05,
0.1,
0.2,
0.25,
0.5,
1.0,
5.0,
10.0,
25.0,
50.0,
100.0,
300.0,
400.0
],
"supportedCoins": [
1,
2,
3
],
"supportedLines": [
1
],
"supportedLimits": [
"game.rtp",
"game.denominations"
],
"rtpSettings": [
{
"gameId": 4,
"setting": 84,
"productId": null,
"default": false,
"minRtp": 83.28,
"maxRtp": 84.71,
"standardDeviation": 8.166990878,
"maxWinMultiplier": 10000.0,
"hitRate": 5.89,
"observedMaxWin": 3333.0,
"observedMillionRounds": 0.2298851,
"freeSpinLimit": null
},
{
"gameId": 4,
"setting": 87,
"productId": null,
"default": false,
"minRtp": 86.13,
"maxRtp": 87.75,
"standardDeviation": 8.144959791,
"maxWinMultiplier": 10000.0,
"hitRate": 5.6,
"observedMaxWin": 3333.0,
"observedMillionRounds": 0.2237136,
"freeSpinLimit": null
}
]
},
"currencyLimitMultiplier": 1,
"currencyScaleMultiplier": 1,
"rtp": 94,
"minJackpotRtp": null,
"maxJackpotRtp": null,
"denominations": [
0.5,
1.0,
5.0
]
}
}
],
"failedGameConfigurations": []
}
Next, you need to generate classes that model the raw JSON data. I like to use:
JSON Utils: Generate C#, VB.Net, SQL TAble and Java from JSON[
^] - Tick "Pascal Case" & set "Property Attributes to "JsonProperty". This will generate the following:
public class RtpSetting
{
[JsonProperty("gameId")]
public int GameId { get; set; }
[JsonProperty("setting")]
public int Setting { get; set; }
[JsonProperty("productId")]
public object ProductId { get; set; }
[JsonProperty("default")]
public bool Default { get; set; }
[JsonProperty("minRtp")]
public double MinRtp { get; set; }
[JsonProperty("maxRtp")]
public double MaxRtp { get; set; }
[JsonProperty("standardDeviation")]
public double StandardDeviation { get; set; }
[JsonProperty("maxWinMultiplier")]
public double MaxWinMultiplier { get; set; }
[JsonProperty("hitRate")]
public double HitRate { get; set; }
[JsonProperty("observedMaxWin")]
public double ObservedMaxWin { get; set; }
[JsonProperty("observedMillionRounds")]
public double ObservedMillionRounds { get; set; }
[JsonProperty("freeSpinLimit")]
public object FreeSpinLimit { get; set; }
}
public class DefaultGameSettings
{
[JsonProperty("gameId")]
public int GameId { get; set; }
[JsonProperty("gId")]
public string GId { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("gameType")]
public string GameType { get; set; }
[JsonProperty("description")]
public string Description { get; set; }
[JsonProperty("refundInactiveBets")]
public bool RefundInactiveBets { get; set; }
[JsonProperty("supportedDenominations")]
public IList<double> SupportedDenominations { get; set; }
[JsonProperty("supportedCoins")]
public IList<int> SupportedCoins { get; set; }
[JsonProperty("supportedLines")]
public IList<int> SupportedLines { get; set; }
[JsonProperty("supportedLimits")]
public IList<string> SupportedLimits { get; set; }
[JsonProperty("rtpSettings")]
public IList<RtpSetting> RtpSettings { get; set; }
}
public class GameConfiguration
{
[JsonProperty("defaultGameSettings")]
public DefaultGameSettings DefaultGameSettings { get; set; }
[JsonProperty("currencyLimitMultiplier")]
public int CurrencyLimitMultiplier { get; set; }
[JsonProperty("currencyScaleMultiplier")]
public int CurrencyScaleMultiplier { get; set; }
[JsonProperty("rtp")]
public int Rtp { get; set; }
[JsonProperty("minJackpotRtp")]
public object MinJackpotRtp { get; set; }
[JsonProperty("maxJackpotRtp")]
public object MaxJackpotRtp { get; set; }
[JsonProperty("denominations")]
public IList<double> Denominations { get; set; }
}
public class GameConfiguration
{
[JsonProperty("gameId")]
public int GameId { get; set; }
[JsonProperty("gameConfiguration")]
public GameConfiguration GameConfiguration { get; set; }
}
public class Result
{
[JsonProperty("gameConfigurationsReturnCount")]
public int GameConfigurationsReturnCount { get; set; }
[JsonProperty("gameConfigurations")]
public IList<GameConfiguration> GameConfigurations { get; set; }
[JsonProperty("failedGameConfigurations")]
public IList<object> FailedGameConfigurations { get; set; }
}
Now we can try and load and deserialize the data:
using System.Diagnostics;
using Newtonsoft.Json;
var rawJson = File.ReadAllText("SampleData.Json");
var results = JsonConvert.DeserializeObject<Result>(rawJson);
If you use the above classes and try to compile, you we see the following errors:
1>C:\proj-Github\~~CodeProject Help\JsonData2\JsonData2\Result.cs(19,21,19,28): error CS1519: Invalid token 'default' in class, record, struct, or interface member declaration
1>C:\proj-Github\~~CodeProject Help\JsonData2\JsonData2\Result.cs(19,34,19,35): error CS1519: Invalid token ';' in class, record, struct, or interface member declaration
1>C:\proj-Github\~~CodeProject Help\JsonData2\JsonData2\Result.cs(19,39,19,40): error CS1519: Invalid token ';' in class, record, struct, or interface member declaration
1>C:\proj-Github\~~CodeProject Help\JsonData2\JsonData2\Result.cs(44,5,44,6): error CS1022: Type or namespace definition, or end-of-file expected
As OriginalGriff pointed out, you need to fix the
Default
name as it is a language keyword.
Now you recompile and the next issue you will see is:
1>C:\proj-Github\~~CodeProject Help\JsonData2\JsonData2\Result.cs(108,18,108,35): error CS0101: The namespace 'JsonData2' already contains a definition for 'GameConfiguration'
1>Done building project "JsonData2.csproj" -- FAILED.
Here are the corrected classes:
using Newtonsoft.Json;
public class RtpSetting
{
[JsonProperty("gameId")]
public int GameId { get; set; }
[JsonProperty("setting")]
public int Setting { get; set; }
[JsonProperty("productId")]
public object ProductId { get; set; }
[JsonProperty("default")]
public bool @Default { get; set; }
[JsonProperty("minRtp")]
public double MinRtp { get; set; }
[JsonProperty("maxRtp")]
public double MaxRtp { get; set; }
[JsonProperty("standardDeviation")]
public double StandardDeviation { get; set; }
[JsonProperty("maxWinMultiplier")]
public double MaxWinMultiplier { get; set; }
[JsonProperty("hitRate")]
public double HitRate { get; set; }
[JsonProperty("observedMaxWin")]
public double ObservedMaxWin { get; set; }
[JsonProperty("observedMillionRounds")]
public double ObservedMillionRounds { get; set; }
[JsonProperty("freeSpinLimit")]
public object FreeSpinLimit { get; set; }
}
public class DefaultGameSettings
{
[JsonProperty("gameId")]
public int GameId { get; set; }
[JsonProperty("gId")]
public string GId { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("gameType")]
public string GameType { get; set; }
[JsonProperty("description")]
public string Description { get; set; }
[JsonProperty("refundInactiveBets")]
public bool RefundInactiveBets { get; set; }
[JsonProperty("supportedDenominations")]
public IList<double> SupportedDenominations { get; set; }
[JsonProperty("supportedCoins")]
public IList<int> SupportedCoins { get; set; }
[JsonProperty("supportedLines")]
public IList<int> SupportedLines { get; set; }
[JsonProperty("supportedLimits")]
public IList<string> SupportedLimits { get; set; }
[JsonProperty("rtpSettings")]
public IList<RtpSetting> RtpSettings { get; set; }
}
public class InnerGameConfiguration
{
[JsonProperty("defaultGameSettings")]
public DefaultGameSettings DefaultGameSettings { get; set; }
[JsonProperty("currencyLimitMultiplier")]
public int CurrencyLimitMultiplier { get; set; }
[JsonProperty("currencyScaleMultiplier")]
public int CurrencyScaleMultiplier { get; set; }
[JsonProperty("rtp")]
public int Rtp { get; set; }
[JsonProperty("minJackpotRtp")]
public object MinJackpotRtp { get; set; }
[JsonProperty("maxJackpotRtp")]
public object MaxJackpotRtp { get; set; }
[JsonProperty("denominations")]
public IList<double> Denominations { get; set; }
}
public class GameConfiguration
{
[JsonProperty("gameId")]
public int GameId { get; set; }
[JsonProperty("gameConfiguration")]
public InnerGameConfiguration InnerGameConfiguration { get; set; }
}
public class Result
{
[JsonProperty("gameConfigurationsReturnCount")]
public int GameConfigurationsReturnCount { get; set; }
[JsonProperty("gameConfigurations")]
public IList<GameConfiguration> GameConfigurations { get; set; }
[JsonProperty("failedGameConfigurations")]
public IList<object> FailedGameConfigurations { get; set; }
}
Now that we have a clear model of the Json Data, we can see that there is a collection of
GameConfigurations
objects.
To get the
Default
property for all
GameConfigurations
, we can do the following:
using System.Diagnostics;
using Newtonsoft.Json;
var rawJson = File.ReadAllText("SampleData.Json");
var results = JsonConvert.DeserializeObject<Result>(rawJson);
var @defaults = results!.GameConfigurations
.SelectMany(x => x
.InnerGameConfiguration
.DefaultGameSettings
.RtpSettings.Select(y => y.Default))
.ToList();
I cover this and a lot more in an article here on codeproject:
Working with Newtonsoft.Json in C# & VB[
^]
Hope this helps!