我正在使用 HttpClient 从链接获取数据。
这是我的回复:
#S7Z OK
#Mon Dec 26 02:26:58 EST 2016
image.anchor=168,186
image.embeddedIccProfile=0
image.embeddedPhotoshopPaths=0
image.embeddedXmpData=0
image.expiration=-1.0
image.height=373
image.iccProfile=sRGB IEC61966-2.1
image.mask=1
image.photoshopPathnames=
image.pixTyp=RGB
image.printRes=72
image.resolution=34
image.thumbRes=17
image.thumbType=2
image.timeStamp=1481737849826
image.width=336
我想将此响应转换为可访问对象。
这是我的 httpclient
工作:
using (var client = getHttpClient())
{
HttpResponseMessage response = await client.GetAsync(path);
if (response.IsSuccessStatusCode)
{
//var imageData = await response.Content.ReadAsAsync<imageData>();
//imageData.timeStamp
}
else
{
//TODO: Need to handle error scenario
}
}
我添加了评论以使您了解我想做什么。实际上,我想从响应中获取 image.timeStamp
值。
谢谢!
最佳答案
您可以通过将响应存储在 Dictionary 中来实现,然后您可以访问任何成员 var x= dic["timeStamp"];
,您还可以通过转换 dic 来扩展实现
到动态对象
。
编辑:
Stream receiveStream = response.GetResponseStream ();
StreamReader readStream = new StreamReader (receiveStream, Encoding.UTF8);
var text = readStream.ReadToEnd();
// Split the content into chunks
foreach(var ch in chunks)
{
string[] kv = ch.Split('=');
dic.Add(kv[0], kv[1]);
}
关于c# - 将响应转换为可访问的对象属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41327953/