Click here to Skip to main content
16,020,114 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a Json as below

{
    "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": []
}



public class GameRtpValue
{
    //[JsonProperty("gameConfigurations/gameId")]
    public int GameId { get; set; }

    //[JsonProperty("rtp")]
    public string Rtp { get; set; }

    public List<RtpValue> RtpValues { get; set; }
}


I want to place setting and default in a class RtpValue

public class RtpValue
	{
		public string Setting { get; set; }

		public string Default { get; set; }
	}


What I have tried:

I have tried the following, but cannot place the default

_gameRtp.RtpValues = jObj.Descendants()
    .OfType<JProperty>()
    .Where(p => p.Name == "setting")
    .Select(p => new RtpValue
    {
        Setting = p.Value.ToString(),
        Default = p.Value.ToString()
    }).ToList()
Posted
Updated 10-Nov-22 9:47am
v2

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:
JavaScript
{
    "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:
C#
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:
C#
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:
C#
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:
C#
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!
 
Share this answer
 
v2
Comments
datt265 11-Nov-22 4:30am    
This was a really well explained answer and I managed to work, I had an attempt to do something similar but I tried to do class manually and nothing was working.

A question in case properties are available in json but the class does not have an entry for them, what would happen ?
Graeme_Grant 11-Nov-22 6:39am    
You are welcome. They would be ignored if not matched otherwise an error is thrown if wrong type.
default is a keyword in C#, you can't use it as a variable name.

Instead, try these classes, and this code for starters:
C#
public class Root
    {
    public List<Setting> Settings { get; set; }
    }

public class Setting
    {
    public int id { get; set; }
    public int setting { get; set; }
    public bool @default { get; set; }
    }
private void MyButton_Click(object sender, EventArgs e)
    {

    }
private void MyOtherButton_Click(object sender, EventArgs e)
    {
    Root jObj = JsonConvert.DeserializeObject<Root>(File.ReadAllText(@"D:\Temp\aaax.json"));
    var x = jObj.Settings.Select(p => new { Setting = p.setting, Default = p.@default });
    }
 
Share this answer
 
Comments
datt265 10-Nov-22 10:56am    
updated my question
OriginalGriff 10-Nov-22 11:57am    
And?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900