A
A
almenovr2021-09-05 20:04:18
C++ / C#
almenovr, 2021-09-05 20:04:18

How to deserialize Json into an object?

There is this kind of JSON

{
    "success": 1,
    "result": {
        "total": 3,
        "limit": 0,
        "offset": null,
        "stocks": [
            {
                "номенклатураGUID": "GUID",
                "складGUID": "GUID",
                "назначениеGUID": "GUID",
                "остаток": 1,
                "rn": 1
            },
            {
                "номенклатураGUID": "GUID",
                "складGUID": "GUID",
                "назначениеGUID": "GUID",
                "остаток": 65,
                "rn": 2
            },
            {
                "номенклатураGUID": "GUID",
                "складGUID": "GUID",
                "назначениеGUID": "GUID",
                "остаток": 13,
                "rn": 3
            }
        ]
    }
}


Created these classes

[DataContract]
  public class Remains
  {
    [DataMember(Name = "success")]
    public int Success { get; set; }
    
    [DataMember(Name = "result")]
    public Result Result { get; set; }
  }
  
  [DataContract]
  public class Result
  {
    [DataMember(Name = "total")]
    public int Total { get; set; }
    
    [DataMember(Name = "limit")]
    public int Limit { get; set; }
    
    [DataMember(Name = "offset")]
    public object Offset { get; set; }
    
    [DataMember(Name = "stocks")]
    public Stocks Stocks { get; set; }
  }
  
  [DataContract]
  public class Stocks
  {
    [DataMember(Name = "номенклатураGUID")]
    public string NomenclatureGuid { get; set; }

    [DataMember(Name = "складGUID")]
    public string WarehouseGuid { get; set; }

    [DataMember(Name = "назначениеGUID")]
    public string AppointmentGuid { get; set; }

    [DataMember(Name = "остаток")]
    public string TotalQuantity { get; set; }
    
    [DataMember(Name = "rn")]
    public int Rn { get; set; }
  }


Desearylize it like this
var result = JsonConvert.DeserializeObject<Result>(response);

I can get everything up to the Stocks object, what is inside I can’t, what is my mistake?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
G
GavriKos, 2021-09-05
@GavriKos

Because you need a List

V
Vasily Bannikov, 2021-09-05
@vabka

#r "nuget:Newtonsoft.Json/13.0.1"

using Newtonsoft.Json;

var json = @"
{
    ""success"": 1,
    ""result"": {
        ""total"": 3,
        ""limit"": 0,
        ""offset"": null,
        ""stocks"": [
            {
                ""номенклатураGUID"": ""GUID"",
                ""складGUID"": ""GUID"",
                ""назначениеGUID"": ""GUID"",
                ""остаток"": 1,
                ""rn"": 1
            },
            {
                ""номенклатураGUID"": ""GUID"",
                ""складGUID"": ""GUID"",
                ""назначениеGUID"": ""GUID"",
                ""остаток"": 65,
                ""rn"": 2
            },
            {
                ""номенклатураGUID"": ""GUID"",
                ""складGUID"": ""GUID"",
                ""назначениеGUID"": ""GUID"",
                ""остаток"": 13,
                ""rn"": 3
            }
        ]
    }
}
";
// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse); 
    public class Stock
    {
        [JsonProperty("номенклатураGUID")]
        public string НоменклатураGUID { get; set; }

        [JsonProperty("складGUID")]
        public string СкладGUID { get; set; }

        [JsonProperty("назначениеGUID")]
        public string НазначениеGUID { get; set; }

        [JsonProperty("остаток")]
        public int Остаток { get; set; }

        [JsonProperty("rn")]
        public int Rn { get; set; }
    }

    public class Result
    {
        [JsonProperty("total")]
        public int Total { get; set; }

        [JsonProperty("limit")]
        public int Limit { get; set; }

        [JsonProperty("offset")]
        public object Offset { get; set; }

        [JsonProperty("stocks")]
        public List<Stock> Stocks { get; set; } //Вот тут у вас просто Stock
    }

    public class Root
    {
        [JsonProperty("success")]
        public int Success { get; set; }

        [JsonProperty("result")]
        public Result Result { get; set; }
    }

var result = JsonConvert.DeserializeObject<Root>(json);

Literally after the code generator everything works fine.

I
Ivan Kiselev, 2014-08-11
@maxpointn2point

I encountered a problem when arp requests from a virtual machine running xen are lost, the loss of IP packets reached 75%, hard binding of the MAC address to the IP address of all virtual machines on the physical server helped. For Debian:
arp -s 192.168.10.11 00:16:3C:13:B2:BF- prescribe the same for all virtual machines and add a pre-up script to /etc/network/interfaces (well, depending on the system)

D
Dmitry Skogorev, 2014-08-11
@EnterSandman

IP address . . . . . . . . . . . . : 10.14.2.55 has
an alias been made to it?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question