我正在使用RestSharp构建.net客户端REST应用程序,我需要使用JSON格式将一些数据发布到服务器(服务器应用程序只能接受JSON)。JSON格式如下:
{ "username": "username", "address": { "address1": "address1", "address2": "address2", "town": "Town", "county": "County", "postCode": "postcode", "country": "United Kingdom" } , "contact": { "telephone": "1234", "fax": "12345", "email": "testemail@test.co.uk"
`} }`我需要将上面的数据作为参数添加到请求对象中,如下所示:
request.AddBody(new { username = "username" });
上面的代码行将使用JSON序列化程序来创建JSON字符串,它工作得很好,但我不确定如何创建下一层(地址,联系人)……所有地址详细信息和联系人详细信息都需要嵌入到地址和联系人元素中。有什么建议吗?
发布于 2012-07-26 03:26:27
你试过了吗?
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: "testemail@test.co.uk" });如果您知道可接受的格式,那么应该会生成所需的json。
如果您在发出请求时运行Fiddler http://www.fiddler2.com/fiddler2/,您可以看到在rest锐化json序列化程序开始工作(通常是json.net,它应该采用上面的序列化程序)之后,您的json是什么格式,并且希望能看到哪里出了问题
https://stackoverflow.com/questions/11646170
复制相似问题