简体   繁体   中英

Using RestSharp to Map JSON to C# Classes

So I know there are probably other solutions to this. But my employer is intent on using the RestSharp package to Deserialize and execute a POST request using JSON Data. I know the data works because I've played with it on the API sandbox. I get a bad request error on debug. Here is the JSON Object in the sandbox:

{
    "stay": {
        "checkIn": "2016-06-08",
        "checkOut": "2016-06-10",
        "shiftDays": "2"
    },
    "occupancies": [
        {
            "rooms": 1,
            "adults": 2,
            "children": 1,
            "paxes": [
                {
                    "type": "AD",
                    "age": 30
                },
                {
                    "type": "AD",
                    "age": 30
                },
                {
                    "type": "CH",
                    "age": 8
                }
            ]
        }
    ],
    "geolocation": {
    "longitude": 2.646633999999949,
    "latitude": 39.57119,
    "radius": 20,
    "unit": "km"
  }
}

and here is my representation in C#:

   var body = new TestRequest
        {

            stay = new Stay
            {
                checkIn = "2016-06-18",
                checkOut = "2016-06-10",
                shiftDays = 2
            },

            occupancies = new Occupancy[]
            {
                new Occupancy
                {
                    rooms = 1,
                    adults = 2, children = 1,
                    paxes = new Pax [] 
                    {
                        new Pax
                        {
                            type = "AD",
                            age = 30
                        },
                        new Pax
                        {
                            type = "AD",
                            age = 30
                        },
                        new Pax
                        {
                            type = "AD",
                            age = 30
                        }
                    }
                }
            },

            geolocation = new Geolocation
            {
                longitude = 2.646633999999949F,
                latitude = 39.57119F,
                radius = 20,
                unit = "km"
            }

        };

What am I doing wrong here? I know there are probably easier ways to do this than RestSharp but I was told this was the method to use....so....help?

Here is the total code in the simple MVC app:

    string signature;

    using (var sha = SHA256.Create())
    {
        long ts = (long)(DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds / 1000;
        Console.WriteLine("Timestamp: " + ts);
        var computedHash = sha.ComputeHash(Encoding.UTF8.GetBytes(apiKey + sharedSecret + ts));
        signature = BitConverter.ToString(computedHash).Replace("-", "");
    }

    var client = new RestClient("https://api.test.hotelbeds.com/");
    var request = new RestRequest("/hotel-api/1.0/hotels", Method.POST);

    var body = new TestRequest
    {

        stay = new Stay
        {
            checkIn = "2016-06-18",
            checkOut = "2016-06-10",
            shiftDays = "2"
        },

        occupancies = new Occupancy[]
        {
            new Occupancy
            {
                rooms = 1,
                adults = 2, children = 1,
                paxes = new Pax [] 
                {
                    new Pax
                    {
                        type = "AD",
                        age = 30
                    },
                    new Pax
                    {
                        type = "AD",
                        age = 30
                    },
                    new Pax
                    {
                        type = "CH",
                        age = 8
                    }
                }
            }
        },

        geolocation = new Geolocation
        {
            longitude = 2.646633999999949F,
            latitude = 39.57119F,
            radius = 20,
            unit = "km"
        }

    };

    request.AddHeader("X-Signature", signature);
    request.AddHeader("Api-Key", apiKey);
    request.AddHeader("Accept", "application/json");
    request.AddHeader("Content-Type", "application/json");
    request.AddParameter("application/json", body, ParameterType.RequestBody);

    IRestResponse response = client.Execute(request);

    return View();
}

Pop your JSON in here: json2csharp . I have used it successfully a number of times to validate. Bad request could be for a number of reasons - do you have a stacktrace or an error description either on the browser debug or in the code?

Have you got their simple sample on the web site up and running? It's a XML example but you can tweak it incrementally to get to where you need to be and help tick off items as working, like your api-key and shared secret being correct. Sometimes these are long strings and you accidentally chop a character or add a space with a cut and paste.

Also, consider dumping the RestSharp and get it working raw, then port it across to keep management happy :)

FYI - your JSON renders the following in json2csharp:

public class Stay
{
    public string checkIn { get; set; }
    public string checkOut { get; set; }
    public string shiftDays { get; set; }
}

public class Pax
{
    public string type { get; set; }
    public int age { get; set; }
}

public class Occupancy
{
    public int rooms { get; set; }
    public int adults { get; set; }
    public int children { get; set; }
    public List<Pax> paxes { get; set; }
}

public class Geolocation
{
    public double longitude { get; set; }
    public double latitude { get; set; }
    public int radius { get; set; }
    public string unit { get; set; }
}

public class RootObject
{
    public Stay stay { get; set; }
    public List<Occupancy> occupancies { get; set; }
    public Geolocation geolocation { get; set; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM