【发布时间】:2019-09-01 08:35:13
【问题描述】:
我有这个代码:
var jsonResponse = response.Content.ReadAsStringAsync().Result;
List<TranslationResult> a = JsonConvert.DeserializeObject< List<TranslationResult>>(jsonResponse);
var t0 = (a[0] != null) ? a[0] : null;
var t1 = (t0 != null) ? t0.Translations[0] : null;
var t2 = (t1 != null) ? t1.DisplayTarget : null;
var p2 = (t1 != null) ? t1.PosTag : null;
public class TranslationResult
{
public string DisplaySource { get; set; }
public Translation[] Translations { get; set; }
}
public class Translation
{
public string DisplayTarget { get; set; }
public string PosTag { get; set; }
}
我用于所有空测试的代码看起来很乱,我想清理一下。任何人都可以提出一种我可以做到这一点的方法,或者如果可能的话,可以提出一种使用 LINQ 的方法。请注意,我只需要 DisplayTarget 和 PosTag 详细信息。
【问题讨论】:
-
与您的问题无关,但请参阅blog.stephencleary.com/2012/07/dont-block-on-async-code.html 关于
.ReadAsStringAsync().Result。