日期:2014-05-17  浏览次数:20451 次

json字符串反序列化成类的集合
如题
json字符串如下:
string JsonStr = @"[{""PlanId"":""201212170001"",""Type"":""Cable"",""PlanContentId"":""9"",""InspectGroupId"":""3"" ,""InspectContentId"":""1"",""InspectContentValue"":""XXX"",""Longitude"":""120.2113312"",""Latitude"":""30.123123"",""InspectDate"":""20121217100330"",""UserId"":""1""},{""PlanId"":""201212170001"",""Type"":""Cable"",""PlanContentId"":""9"",""InspectGroupId"":""3"" ,""InspectContentId"":""1"",""InspectContentValue"":""XXX"",""Longitude"":""120.2113312"",""Latitude"":""30.123123"",""InspectDate"":""20121217100330"",""UserId"":""1""},{""PlanId"":""201212170001"",""Type"":""Cable"",""PlanContentId"":""9"",""InspectGroupId"":""3"" ,""InspectContentId"":""1"",""InspectContentValue"":""XXX"",""Longitude"":""120.2113312"",""Latitude"":""30.123123"",""InspectDate"":""20121217100330"",""UserId"":""1""}]";

我新建了一个实体类,想读取这些数据,然后放到集合中。试了好几个方法都不行,基本都报错。  单引号就可以,双引号就不行了。但传递的json格式已经规定好了,就是这样的。


求解决方案。

------解决方案--------------------
引用:
引用:用JSON序列化和反序列化的类就行了,再说,JSON用单引号较好。

public static List<T> JSONStringToList<T>(string JsonStr)
        {
            JavaScriptSerializer Serializer = new JavaScriptS……

正确的
"[{""PlanId"":""201212170001"",""Type"":""Cable"",""PlanContentId"":""9"",""InspectGroupId"":""3"",""InspectContentId"":""1"",""InspectContentValue"":""XXX"",""Longitude"":""120.2113312"",""Latitude"":""30.123123"",""InspectDate"":""20121217100330"",""UserId"":""1""},
{""PlanId"":""201212170001"",""Type"":""Cable"",""PlanContentId"":""9"",""InspectGroupId"":""3"",""InspectContentId"":""1"",""InspectContentValue"":""XXX"",""Longitude"":""120.2113312"",""Latitude"":""30.123123"",""InspectDate"":""20121217100330"",""UserId"":""1""},
{""PlanId"":""201212170001"",""Type"":""Cable"",""PlanContentId"":""9"",""InspectGroupId"":""3"",""InspectContentId"":""1"",""InspectContentValue"":""XXX"",""Longitude"":""120.2113312"",""Latitude"":""30.123123"",""InspectDate"":""20121217100330"",""UserId"":""1""}]"

你的错误的
"[{""PlanId"":""201212170001"",""Type"":""Cable"",""PlanContentId"":""9"",""InspectGroupId"":""3"" ,""InspectContentId"":""1"",""InspectContentValue"":""XXX"",""Longitude"":""120.2113312"",""Latitude"":""30.123123"",""InspectDate"":""20121217100330"",""UserId"":""1""},{""PlanId"":""201212170001"",""Type"":""Cable"",""PlanContentId"":""9"",""InspectGroupId"":""3"" ,""InspectContentId"":""1"",""InspectContentValue"":""XXX"",""Longitude"":""120.2113312"",""Latitude"":""30.123123"",""InspectDate"":""20121217100330"",""UserId"":""1""},{""PlanId"":""201212170001"",""Type"":""Cable"",""PlanContentId"":""9"",""InspectGroupId"":""3"" ,""InspectContentId"":""1"",""InspectContentValue"":""XXX"",""Longitude"":""120.2113312"",""Latitude"":""30.123123"",""InspectDate"":""20121217100330"",""UserId"":""1""}]";

把这两部分贴到能显示半角全角的编辑器中看看区别。空格和全角都有