使用C#.NET将json字符串转换为datatable?

时间:2016-03-23 11:15:06

标签: asp.net json c#-4.0 datatable

当我传递整数值然后JsonData转换为数据表.. 当我传递十进制值时,我收到错误.. 输入字符串的格式不正确。无法存储< 1.5>在增值税栏中。

{{1}}

这是我的JSON数据 [{" STATEID":1," VAT":0," CST":0}, {" STATEID":2" VAT":" 1.5"" CST":" 1.5"} , {" STATEID":3," VAT":0," CST":0},{" STATEID&#34:4,&#34 ; VAT":0," CST":0},{" STATEID":5," VAT":0," CST&#34 ;:0},{" STATEID":6," VAT":0," CST":0},{" STATEID&#34 ;: 7," VAT":0," CST":0},{" STATEID":8," VAT":0,&# 34; CST":0},{" STATEID":9," VAT":0," CST":0},{" STATEID":10," VAT":0," CST":0},

1 个答案:

答案 0 :(得分:0)

protected void Page_Load(object sender, EventArgs e)
    {
        string jsonData = "[{\"Stateid\":1,\"VAT\":\"0\",\"CST\":\"0\"}, {\"Stateid\":2,\"VAT\":\"1.5\",\"CST\":\"1.5\"}, {\"Stateid\":3,\"VAT\":\"0\",\"CST\":\"0\"},{\"Stateid\":4,\"VAT\":\"0\",\"CST\":\"0\"},{\"Stateid\":5,\"VAT\":\"0\",\"CST\":\"0\"},{\"Stateid\":6,\"VAT\":\"0\",\"CST\":\"0\"},{\"Stateid\":7,\"VAT\":\"0\",\"CST\":\"0\"},{\"Stateid\":8,\"VAT\":\"0\",\"CST\":\"0\"},{\"Stateid\":9,\"VAT\":\"0\",\"CST\":\"0\"}]" ;
        var dt = JsonConvert.DeserializeObject<DataTable>(jsonData);
    }

我发现您的增值税在第一条记录中标记为int,但之后在字符串中标记为1.5,因此它会给您错误

所以我转换字符串中的所有数据并尝试完成。