如题!还有json数据在传回服务器端后,你是如何进行数据验证的呢??
我要的键值对和数据验证!!!给个例子,下面的日期是错误的,如何验证?
[
{
"id": 0,
"guid": "3664e660-865f-4968-921a-48a0201a474b",
"isActive": true,
"balance": "$3,057.00",
"picture": "http://placehold.it/32x32",
"age": 29,
"name": "Schwartz Chapman",
"gender": "male",
"company": "Polaria",
"email": "schwartzchapman@polaria.com",
"phone": "+1 (966) 467-2302",
"address": "474 Kossuth Place, Lupton, Oregon, 3855",
"about": "Nisi et eu consectetur enim magna aliquip voluptate dolore aliquip commodo aliquip ullamco. Ad quis occaecat pariatur reprehenderit nisi adipisicing excepteur qui. Aute ipsum officia incididunt in ipsum.\r\n",
"registered": "2007-01-26aaaaT20:03:19 -08:00",
"latitude": -51.695203,
"longitude": 40.777699,
"tags": [
"adipisicing",
"excepteur",
"Lorem",
"non",
"est",
"eiusmod",
"est"
],
"friends": [
{
"id": 0,
"name": "Mccall Jarvis"
},
{
"id": 1,
"name": "Carol Hardy"
},
{
"id": 2,
"name": "Adrian Stein"
}
],
"randomArrayItem": "cherry"
},
{
"id": 1,
"guid": "8f3c628c-9b52-4ee6-843c-d3d0208e6957",
"isActive": false,
"balance": "$1,562.00",
"picture": "http://placehold.it/32x32",
"age": 27,
"name": "Elnora Bryan",
"gender": "female",
"company": "Zoid",
"email": "elnorabryan@zoid.com",
"phone": "+1 (850) 590-3345",
"address": "867 Emerson Place, Macdona, Mississippi, 7208",
"about": "Dolore eiusmod officia consequat nisi consequat cillum incididunt nulla aute qui sit ut laboris. Dolore aliqua esse aute tempor adipisicing reprehenderit excepteur cupidatat irure aute. Sint pariatur do magna exercitation excepteur. Nostrud officia eiusmod minim pariatur pariatur pariatur exercitation quis tempor esse dolore mollit. Quis anim deserunt anim ad ut tempor. Reprehenderit consectetur aliqua eiusmod sunt pariatur cupidatat magna do elit do culpa.\r\n",
"registered": "2013-05-16bbbT10:22:39 -08:00",
"latitude": 31.870666,
"longitude": -118.462993,
"tags": [
"veniam",
"tempor",
"aute",
"nulla",
"commodo",
"qui",
"minim"
],
"friends": [
{
"id": 0,
"name": "Washington Rich"
},
{
"id": 1,
"name": "Turner Riddle"
},
{
"id": 2,
"name": "Ann Glover"
}
],
"randomArrayItem": "cherry"
}
]
要求在服务端给出友好的验证提示!!
http://json.codeplex.com
小学生只能帮你到这里了!
我老早就知道了,就是不知道这个怎么对json数据进行验证
@春色园: 我猜测的哈,我估计当将把JSON序列化的时候如果不是有效JSON数据会报异常的!!
你还是别来凑热闹了,大湿
@春色园: 额,自便,我只是热心的希望能帮你解决问题罢了!
你所说的日期是错误的是什么意思,是它不是一个日期呢,还是不在你的日期范围内呢?反序列化失败时会提示,但这要求你服务端有对应JSON的反序列化类型。键值对不太可能,你看看你上面的类型,是一个树型结构,又不是一个二叉树,没法转换成你要的键值对。
之前听同事说nodejs已经支持服务端运行javascript来解析javascript,但没有研究过。据我据知,服务端没有太好的办法。但有google提供的一个服务端javascript引擎,我想能解析应该可以解析json吧。
public static T ParseJsonOrDefault<T>(string json)
{
return ParseJsonOrDefault<T>(json, default (T));
}