0

I have this JSON response and I create an equivalent C# class. But when I try to convert this JSON to c# result not completed.

JSON Response

{
    "result": "success",
    "totalresults": 1,
    "startnumber": 0,
    "numreturned": 1,
    "orders": {
        "order": [
            {
                "id": 26,
                "ordernum": 9658502986,
                "userid": 18,
                "frauddata": "",
                "lineitems": {
                    "lineitem": [
                        {
                            "type": "domain",
                            "relid": 9,
                            "dnsmanagement": 0,
                            "emailforwarding": 0,
                            "idprotection": 0
                        }
                    ]
                }
            }
        ]
    }
}

C# Class

public class RootObject
    {
        int totalresults { get; set; }
        int startnumber { get; set; }
        int numreturned { get; set; }
        orders orders { get; set; }
    }
    public class lineitem
    {
        public string type { get; set; }
        public int idprotection { get; set; }
    }

    public class lineitems
    {
        public List<lineitem> lineitem { get; set; }
    }

    public class order
    {
        public int id { get; set; }
        public long ordernum { get; set; }
        public int userid { get; set; }
        public string fraudmodule { get; set; }
        public string fraudoutput { get; set; }
        public string frauddata { get; set; }
        public lineitems lineitems { get; set; }
    }

    public class orders
    {
        public List<order> order { get; set; }
    }

I used JsonConvert.DeserializeObject but in result orders property always empty!

enter image description here

I need to orders property. Can anybody help me?

Sahar Sarikhani
  • 27
  • 1
  • 11
  • Is that really the model being returned? With a object property called "orders" and an array called "order" nested in that property? I assume it is, but that is a very confusing model. – Nick Bailey Jul 25 '18 at 18:20
  • Also, in your example, you're working with a RestSharp.RestResponse not a raw JSON string. RestSharp automatically deserializes your JSON for you when you use one of those generic methods. – Nick Bailey Jul 25 '18 at 18:22
  • The properties of `RootObject` are private. You need to make them public, as explained in [Why are some members missing when trying to print an object by serializing to JSON?](https://stackoverflow.com/q/48156976/3744182). (The answer to that question has some workarounds if you cannot make the members public.) In fact I think this is a duplicate; agree? – dbc Jul 25 '18 at 19:58

1 Answers1

0

You should try like this;

JsonConvert.DeserializeObject<RootObject>(yourJsonString)
Mehmet
  • 1,824
  • 3
  • 19
  • 28
  • Won't help, because class definition does not match json object – derpirscher Jul 25 '18 at 20:29
  • Class definition is match with json object! i reduce properties in class and json in this post! – Sahar Sarikhani Jul 26 '18 at 04:57
  • 1
    @SaharSarikhani I tired with your class definition. Everything is ok with this Json and Class definition. I can see orders on debug. – Mehmet Jul 26 '18 at 05:48
  • Yes, but i used interface for rootobject and with interface orders is empty always. Any way i use class now ,thank you.but when lineitem in return object is empty jsonconvert has error. – Sahar Sarikhani Jul 26 '18 at 16:34