0

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);
4
  • Here the answer to your question: newtonsoft.com/json/help/html/DeserializeObject.htm You wrote that you tried something and did not manage. What was the error? Commented Nov 14, 2017 at 15:51
  • Hi, your JSON structure features a dictionary for your "data" child. If you want C# to correctly deserialize the object, try making your json in the following structure: "data": [ { ...object values ...}, { another object} ] Commented Nov 14, 2017 at 15:51
  • You should try something like RetornoConsumo _featuredArticles = JsonConvert.DeserializeObject<RetornoConsumo>(json); Commented Nov 14, 2017 at 15:55
  • Copy Json content -> go to Visual studio -> Create new c# class File -> Edit menu -> Paste Special -> Paste Json as classes... Job done Commented Nov 14, 2017 at 16:06

1 Answer 1

2

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

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

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.