用JSON序列化和反序列化的类就行了,再说,JSON用单引号较好。
解决方案 »
- 请教,很多网站首页是index.htm,页面上的列表数据是如何实时更新的?
- 求正则表达式匹配非链接(非<>)
- tencent://message/?uin=10002怎么实现的?
- TreeView 的问题
- 如何将GridView的值取出并赋值?
- 100分求DATASET中两个记录集的提取以及 asp.net2.0 环境自定义分页控件 解决问题马上给反结贴
- 在出100分:http://community.csdn.net/Expert/topic/4400/4400615.xml?temp=.3651697
- 发现 asp.net C# 一级服务器兼容问题
- 问个foreach的问题?
- 各位讨论一下web服务器控件和Html控件吧,我现在越来越迷茫,来着有分!
- post传参,当字符过长会被截取
- 关于dropdownlist的问题,求救!
public static List<T> JSONStringToList<T>(string JsonStr)
{
JavaScriptSerializer Serializer = new JavaScriptSerializer(); List<T> objs = Serializer.Deserialize<List<T>>(JsonStr); return objs;
}
我用了这个反序列化的方法,但是报错:
输入的对象无效。应该是“:”或“}”
json格式不是双引号才正规么?
单引号我也想啊,但是我只是接收json而已,格式是那边定的
/// <summary>
/// JSON反序列化
/// </summary>
public static T JsonDeserialize<T>(string jsonString)
{ //将"yyyy-MM-dd HH:mm:ss"格式的字符串转为"\/Date(1294499956278+0800)\/"格式 string p = @"\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}"; MatchEvaluator matchEvaluator = new MatchEvaluator(ConvertDateStringToJsonDate); Regex reg = new Regex(p); jsonString = reg.Replace(jsonString, matchEvaluator); DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T)); using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString)))
{
T obj = (T)ser.ReadObject(ms);
return obj;
}
}
/// <summary>
/// 将时间字符串转为Json时间
/// </summary>
private static string ConvertDateStringToJsonDate(Match m)
{ string result = string.Empty; DateTime dt = DateTime.Parse(m.Groups[0].Value); dt = dt.ToUniversalTime(); TimeSpan ts = dt - DateTime.Parse("1970-01-01"); result = string.Format("\\/Date({0}+0800)\\/", ts.TotalMilliseconds); return result; }调用 JsonDeserialize<model>(json字符串);
using System.Runtime.Serialization.Json;
public static T JsonDeserialize<T>(string jsonString)
{
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString)))
{
return (T)ser.ReadObject(ms);
}
}
List<model> list = JsonDeserialize<List<model>>(str);
{""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 = t.Groups["PlanId"].Value,
Type = t.Groups["Type"].Value,
PlanContentId = t.Groups["PlanContentId"].Value,
InspectGroupId = t.Groups["InspectGroupId"].Value,
InspectContentId = t.Groups["InspectContentId"].Value,
InspectContentValue = t.Groups["InspectContentValue"].Value,
Longitude = t.Groups["Longitude"].Value,
Latitude = t.Groups["Latitude"].Value,
InspectDate = t.Groups["InspectDate"].Value,
UserId = t.Groups["UserId"].Value
}).ToArray();
{
Dictionary<string, string> dict = new Dictionary<string, string>();
foreach (var m in Regex.Matches(t.Value, @"[""']([^""']+)[""']:[""']([^""']+)[""'](?=,|})").OfType<Match>().Select(tt =>
new
{
key = tt.Groups[1].Value,
value = tt.Groups[2].Value
}))
dict.Add(m.key, m.value);
return dict;
}).ToArray();
参数有什么作用?
http://www.suchso.com/UIweb/json2-js-stringify-parse-serializable-deserialize.html