Tiedt Tech
Tiedt Tech

Reputation: 717

Json.Net deserialize JSON objects

I've popped consuming a WebServer returning the Json bellow, and I'm not able to convert it to an object.

Json

{
    "success":true,
    "data":{
        "24486146360":{
            "rfid":"123465789456",
            "products":[
            {
                "sale_id":35,
                "quantity":2,
                "price":"1",
                "total":"2",
                "unit":"uni",
                "sku":14
            },
            {
                "sale_id":36,
                "quantity":2,
                "price":"2.5",
                "total":"5",
                "unit":"uni",
                "sku":17
            }
            ]
        },
        "24758345953":{
            "rfid":"2129",
            "products":[
            {
                "sale_id":39,
                "quantity":1,
                "price":"10",
                "total":"10",
                "unit":"ml",
                "sku":19998
            }
            ]
        },
        "64577015900":{
            "rfid":"1934",
            "products":[
            {
                "sale_id":40,
                "quantity":1,
                "price":"10",
                "total":"10",
                "unit":"ml",
                "sku":19998
            }
            ]
        },
        "56768990934":{
            "rfid":"1746",
            "products":[
            {
                "sale_id":46,
                "quantity":1,
                "price":"8.00",
                "total":"8",
                "unit":"UN",
                "sku":20
            }
            ]
        }
    }
}

I used json2sharp to generate a class from the JSON, I then cleaned up the class and was left with the following:

My Class create help for json2sharp

public class Consumo
{
    public string rfid { get; set; }
    public List<ConsumoProduto> products { get; set; }
}

public class ConsumoProduto
{
    public int sale_id { get; set; }
    public double quantity { get; set; }
    public string price { get; set; }
    public string total { get; set; }
    public string unit { get; set; }
    public int sku { get; set; }
}

public class RetornoConsumo
{
    [JsonProperty("success")]
    public bool Processado { get; set; }
    [JsonProperty("data")]
    public List<Consumo> Registro { get; set; }
}

My Problem

how do I convert Json to a valid object using Json.Net?

Test

I tried to do this and I could not

Dictionary<string, RetornoConsumo> _featuredArticles = JsonConvert.DeserializeObject<Dictionary<string, RetornoConsumo>>(json);

Upvotes: 0

Views: 127

Answers (1)

Brian Rogers
Brian Rogers

Reputation: 129807

In your RetornoConsumo class, the Registro property needs to be a Dictionary<string, Consumo> not a List<Consumo>.

public class RetornoConsumo
{
    [JsonProperty("success")]
    public bool Processado { get; set; }
    [JsonProperty("data")]
    public Dictionary<string, Consumo> Registro { get; set; }
}

Then, you need to deserialize the JSON into the RetornoConsumo class:

var data = JsonConvert.DeserializeObject<RetornoConsumo>(json);

Fiddle: https://dotnetfiddle.net/vfhdXp

Upvotes: 2

Related Questions