2

このフォロー オブジェクトを ajax を介して asp.net Web サーバーに投稿しています。

$.ajax({
   type: "POST",
   url: "/home.aspx",
   contentType:'application/json',
   data:JSON.stringify({d1:42.00,d2:3.14,d3:'17'})
});

JSON.Net がオブジェクトを逆シリアル化するとき

JsonConvert.DeserializeObject<DataTable>(stringfied);

d2 と d3 を int として表示すると、結果は d1 = 42、d2 = 3、d3 = 17 になります。

問題はこの投稿とまったく同じです: http://digitalbush.com/2011/04/24/asp-net-mvc3-json-decimal-binding-woes/

Web フォームを使用しているため、この記事を使用してこの問題を回避できませんでした。

方法はありますか?

アップデート

実際には上記の文字列は間違っています。これは問題を反映しています。Linqpad で再現しました。

JsonConvert.DeserializeObject<DataTable>("[{\"Price\":3},{\"Price\":3.33}]").Dump();

datatable の結果: 3 と 3 ではなく 3 と 3.33

メソッドを使用DeserializeObjectすると正常に動作しますが、アプリに Datatable を MSExcel に変換する汎用関数があるため、Datatable を使用する必要があります。

4

1 に答える 1

1

このような単純なデータで、なぜ使用しませんGeneric.Dictionaryか?

string json = "{d1:42.00, d2:3.14, d3:\"17\"}";

Web.Script.Serialization.JavaScriptSerializer serializer = new Web.Script.Serialization.JavaScriptSerializer();
System.Collections.Generic.Dictionary<string, object> jsonObject = (System.Collections.Generic.Dictionary<string, object>)serializer.DeserializeObject(json);

decimal d1 = jsonObject["d1"];
decimal d2 = jsonObject["d2"];
string d3 = jsonObject["d3"];
于 2013-02-08T23:34:30.007 に答える