Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

JsonSerializer.Deserialize always returns Null

Trying to deserialize the following json string:

string json = "{\"d\":{\"__metadata\":{\"id\":\"http://my.dev.int:8000/sap/opu/odata/sap/ZFIORI_SERIAL_NUMBERS_SRV/MATERIALSet('250')\",\"uri\":\"http://my.dev.int:8000/sap/opu/odata/sap/ZFIORI_SERIAL_NUMBERS_SRV/MATERIALSet('250')\",\"type\":\"ZFIORI_SERIAL_NUMBERS_SRV.MATERIAL\"},\"MATNR\":\"250\",\"MAKTX\":\"X:K10/MF250\"}}";

into Class Object

namespace Scanner.Model
{
    public class Material
    {
        public string MATNR { get; set; }
        public string MAKTX { get; set; }
    }
}

I have tried using many ways including :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Material material = JsonSerializer.Deserialize<Material>(json, new JsonSerializerOptions
{
    PropertyNameCaseInsensitive = true,
    IgnoreNullValues = true,
    PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
});


Console.WriteLine(material.MATNR); //expected output: 250
Console.WriteLine(material.MAKTX); //expected  output: X:K10/MF250 

But material.MATNR and material.MAKTX are always null .

>Solution :

I think you need to familiarize yourself more with the Json structure, Material is not the type of object being returned in your Json string, it is a nested class of the main object, I have put together a dot net fiddle and tested that this will work for you, as you can see, the main object is what I called MyJson which holds one property of class type MetaData and a property name of d, this class then contains 3 properties: of type MetaBase which contains 3 properties of string types and 2 other string properties, I then deserialize the json string to the main object type of MyJson and then write that property to the console window, as you can see this prints 250, I’m also using, JsonPropertyNameAttribute to set certain class properties to the matching values in the json string, you can read more about that here:system.text.json.serialization.jsonpropertynameattribute
fiddle here: dotnet-fiddle:

using System;
using System.Text.Json;
using System.Text.Json.Serialization;
public class Program
{
    public static void Main()
    {
        string json = "{\"d\":{\"__metadata\":{\"id\":\"http://my.dev.int:8000/sap/opu/odata/sap/ZFIORI_SERIAL_NUMBERS_SRV/MATERIALSet('250')\",\"uri\":\"http://my.dev.int:8000/sap/opu/odata/sap/ZFIORI_SERIAL_NUMBERS_SRV/MATERIALSet('250')\",\"type\":\"ZFIORI_SERIAL_NUMBERS_SRV.MATERIAL\"},\"MATNR\":\"250\",\"MAKTX\":\"X:K10/MF250\"}}";
        var myjson = System.Text.Json.JsonSerializer.Deserialize<MyJson>(json);
        Console.WriteLine(myjson.D.MATNR);
    }
}

public class MyJson
{
    [JsonPropertyName("d")]
    public MetaData D {get; set;}
}

public class MetaData
{
   [JsonPropertyName("__metadata")]
   public MetaBase MetaB {get; set;}
   public string MATNR { get; set; }
   public string MAKTX { get; set; }    
    
}

public class MetaBase
{
   [JsonPropertyName("id")]
   public string ID {get; set;}
   [JsonPropertyName("uri")]
   public string Uri {get; set;}
   [JsonPropertyName("type")]
   public string Type {get; set;}
    
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading