JSON 応答を期待するメソッド内で単純な API 呼び出しを作成しました... 目を見張るものはありません:
// [API call...]
dynamic responseString = JsonConvert.DeserializeObject(response.Result.Content.ReadAsStringAsync().Result);
item.category = responseString.category;
item.shortDescription = responseString.shortDescription;
item.countryOfManufacture = responseString.manufacturerCountry ?? "DE";
利用可能なAPIからのすべての必要なパラメータがない場合があります...そのため、ユーザーが欠落している値を入力できるダイアログを確認して表示しようis null
としました...is string.Empty
でもこれは:
if (responseString.weight == null ||responseString.weight == string.Empty )
{
DialogArgs args = new DialogArgs();
args.Add("Gewicht");
OnMissingValue?.Invoke(args);
item.weight = args.Get<float>("Gewicht");
}
else
{
item.weight = Convert.ToSingle(responseString.weight) / 1000;
}
また
if (string.IsNullOrEmpty(responseString.weight))
FormatExceptionをスローします。is null
どちらかをチェックするis string.Empty
と、魅力的に機能します。私は参照型と値型の違いを知っており、おそらく問題があると思いました...ただし、なぜこのように動作するのかを知りたいです...
前もって感謝します...そして私の英語でごめんなさい...
マーカス