0

I did search but seem like i didnt find any solution for this... This is my sample Json:

{
  "WSettings":{
  "Message":"\"We are the universe's way of experiencing itself.\"<br/>- Kurzgesagt -",
  "universalMessageProcChance":10,
  "Hi":{
         "altName":"Hi",
         "normalState":"hi",
         "hState":"hi_h",
         "aState":"hi_a",
         "background":"bg_owo_r",
         "stateLayout":4,
         "backgroundLayout":1,
         "eventMessage":"Da"
     },
  "Yu":{
         "altName":"Al",
         "normalState":"al",
         "hState":"al_h",
         "aState":"ali_a",
         "background":"bg_owo_b",
         "stateLayout":4,
         "backgroundLayout":1,
         "eventMessage":"Da"
    }
  }
}

This is my classes:

public class WSettings
{
    public string Message { get; set; }
    public int universalMessageProcChance { get; set; }

    [JsonProperty("Hi")]
    public StateW Hi { get; set; }

    [JsonProperty("Yu")]
    public StateW Yu { get; set; }
}

public class StateW
{
    public string altName { get; set; }
    public string normalState { get; set; }
    public string hState { get; set; }
    public string aState { get; set; }
    public string background { get; set; }
    public int stateLayout { get; set; }
    public int backgroundLayout { get; set; }
    public string eventMessage { get; set; }
}

and this to deserialize:

var w = Newtonsoft.Json.JsonConvert.DeserializeObject<WSettings>(File.ReadAllText(@"json.txt"));

It returns all attributes as null. I honestly dont know what is wrong there so please pardon my ignorant. Any help is appericated :)

1
  • 1
    "WSettings" is the property of an object. you need that object to deserialize it public class Rootobject { public Wsettings WSettings { get; set; }} then DeserializeObject<Rootobject> Commented Nov 8, 2021 at 8:28

3 Answers 3

1

You'll need another wrapper class on WSettings.
Try something like this:

public class WSettingsParent
{
    public WSettings WSettings { get; set; }
}

And deserialize like this:

WSettingsParent myDeserializedClass = JsonConvert.DeserializeObject<WSettingsParent>(File.ReadAllText(@"json.txt")));

Here's a useful tool for checking that you can use to make sure the structure of your classes are properly set:
https://json2csharp.com/
Just paste your json on the left side and it will generate classes accordingly. (You might need some slight modifications but the big picture is visible there).

Sign up to request clarification or add additional context in comments.

Comments

1

If you look at your JSON closely, WSettings is wrapped under one open-close curly brackets. It denotes that WSetting is also one property of top/Root level JObject.

Create new Root class and include WSettings as a property of it and deserialize your json with this Root class

Additional class:

public class Root
{
   //Property of existing WSettings class   
   public WSettings WSettings { get; set; }
}

Deserialization:

using Newtonsoft.Json;
....

var w = JsonConvert.DeserializeObject<Root>(File.ReadAllText(@"json.txt"));
                                   //^^^^^^ Use Root class instead of WSettings

Comments

0

Already a good answer is provided. You can also try to use a JSON parser. its working

var jsparse = JObject.Parse(File.ReadAllText(@"json.txt"));  
        var w = JsonConvert.DeserializeObject<WSettings>(jsparse["WSettings"].ToString());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.