Home > OS >  JsonConvert.DeserializeObject returning all vaules null
JsonConvert.DeserializeObject returning all vaules null

Time:10-05

My brain burning problem: The deserialize code from newtonsoft.json only returns null on all strings: enter image description here

Also i checked if the variables are exactly named as the one in the json, they are. This here is my Deserialize Code:

StreamReader r = new StreamReader(filepath);
string jsonString = r.ReadToEnd();
GlobalVariables.ItemSer m = JsonConvert.DeserializeObject<GlobalVariables.ItemSer>(jsonString);
Console.WriteLine(GlobalVariables.ItemSer.ARSID); //breakpointed here
GlobalVariables.ApplicationState.loadjson = true;

These are the Variables from the GlobalVariable class:

[Serializable]
public class ItemSer
{
    public static string EssentialID { get; set; }
    public static string MotherID { get; set; }
    public static string SimpleID { get; set; }
    public static string PassiveID { get; set; }
    public static string ARSID { get; set; }
}

And the json that is getting deserialized:

{
"config":
[
    {
    "EssentialID": "5500",
    "MotherID": "6600",
    "SimpleID": "55",
    "PassiveID": "870155",
    "ARSID": "00551100"
    },
    {
    "EssentialID": "6600",
    "MotherID": "5500",
    "SimpleID": "66",
    "PassiveID": "870166",
    "ARSID": "00661100"
    }
]

}

i really don't know why the vaules are returning null, so i choosed to write a question instead wasting more time into googling.

CodePudding user response:

In the JSON, there is a property config that holds a list of objects. When deserializing, the code tries to match this JSON structure to the ItemSer class. The JSON must match the class structure very closely and does not pick the first matching part of the JSON.

So you need to adjust the class structure to the JSON so that the deserializer can connect the two:

public class Container
{
  [JsonProperty("config")]
  public IList<ItemSer> Config { get; set; }
}

// ...

var container = JsonConvert.DeserializeObject<Container>(jsonString);
var itemSer = container.Config.FirstOrDefault();

CodePudding user response:

Your JSON is an object with single array property config. You need another structure to deserialize it right:

public class ItemsModel 
{
  public List<ItemSer> config {get;set;}
}

then

var model = JsonConvert.DeserializeObject<ItemsModel>(jsonString);
var m = model.config.FirstOrDefault();

CodePudding user response:

You can just parse your json, and deserialize only a list

List<GlobalVariables.ItemSer> config = JObject.Parse(jsonString)["config"]
                                .ToObject<List<GlobalVariables.ItemSer>>(); 

CodePudding user response:

You miss the type of the json. Your deserializer looks for there is an object. But there is an array. Please try wrap your model with List<>

The example of model classes:

    public class Item
    {
        public string EssentialID { get; set; }
        public string MotherID { get; set; }
        public string SimpleID { get; set; }
        public string PassiveID { get; set; }
        public string ARSID { get; set; }
    }

    public class Config
    {
        public List<Item> ItemList{ get; set; }
    }

And access:

StreamReader r = new StreamReader(filepath);
string jsonString = r.ReadToEnd();
Config m = JsonConvert.DeserializeObject<Config>(jsonString);
Console.WriteLine(Config.Items.FirstOrDefault().ARSID);
GlobalVariables.ApplicationState.loadjson = true;
  • Related