damian996
damian996

Reputation: 1

RestSharp JSON serialization

I am building .net client REST application using RestSharp and I need to post some data to the server using JSON format (the server app can only accept JSON). The JSON format is as below:

{ "username": "username", "address": { "address1": "address1", "address2": "address2", "town": "Town", "county": "County", "postCode": "postcode", "country": "United Kingdom" } , "contact": { "telephone": "1234", "fax": "12345", "email": "[email protected]"
} }

I need to add the data above as parameters to the request object like below:

request.AddBody(new { username = "username" });

The line above will use JSON serializer to create a JSON string and it works fine but I am not sure how to create next levels (address, contact)... All address details and contact details need be embedded in the address and contact elements. Any advice?

Upvotes: 0

Views: 1909

Answers (1)

PJUK
PJUK

Reputation: 1856

Have you tried

request.AddBody(new { username = "username", 
address: new { address1: "address1", address2: address2, "town": Town, county: "County", postCode: "postcode", country: United Kingdom } , 
contact: new { telephone: "1234", fax: "12345", email: "[email protected]" });

?

If you know the accepted format, that should generate the required json.

If you run Fiddler http://www.fiddler2.com/fiddler2/ as you make the request you can see what format your json is in after the rest sharp json serialiser kicks in (usually json.net which should take the above) and hopefully see where it going wrong

Upvotes: 1

Related Questions