为什么此 JSON 返回“无效的 JSON 原语”?
以下 JSON 未反序列化。显然是因为 DECIMALS 中保存的是 JSON。我该如何解决这个问题?
此初始 JSON 来自服务器并且有效:
{
"AppropriationAmount": 25000000,
"AppropriationHours": 56300,
"ArrThreshold": 11,
"ClientKey": 24,
"Description": 'Find and incarcerate the escaped prisoner',
"DirectHours": 50000,
"EndDate": '3/31/2011',
"EngineeringHours": 4000,
"IndirectHours": 2000,
"Key": 1589,
"Number": '0',
"OtherHours": 300,
"ProductivityCurveType": 'BurnedEarned',
"ProjectManager": 'Doctor Who',
"ProjectName": 'Prisoner ZERO',
"StartDate": '5/1/2010'
}
发送到服务器的后续 JSON 失败:
一旦用户编辑表单,数据就会在客户端序列化并发送回...尝试反序列化 JSON 时会失败。
{
"AppropriationAmount": 56300.00,
"AppropriationHours": 25000000.00,
"ArrThreshold": 11.00,
"ClientKey": ,
"Description": 'Find and incarcerate the escaped prisoner',
"DirectHours": 50000.00,
"EndDate": '3/31/2011',
"EngineeringHours": 4000.00,
"IndirectHours": 2000.00,
"Key": 1589,
"Number": '0',
"OtherHours": 300.00,
"ProductivityCurveType": 'BurnedEarned',
"ProjectManager": 'Doctor Who',
"ProjectName": 'Prisoner ZERO',
"StartDate": '5/1/2010'
}
此代码抛出错误:
try
{
if (!String.IsNullOrEmpty(this.JSON))
{
serializer = new JavaScriptSerializer();
dialog = serializer.Deserialize<ProjectDecorator>(this.JSON);
}
}
catch (Exception ex)
{
// The message shows here
}
抛出的错误如下所示:
{"Invalid JSON primitive: ."}
The following JSON is not deserializing. It's obviously because the DECIMALS in the saves JSON. How do I fix this?
This initial JSON comes from the server and IS VALID:
{
"AppropriationAmount": 25000000,
"AppropriationHours": 56300,
"ArrThreshold": 11,
"ClientKey": 24,
"Description": 'Find and incarcerate the escaped prisoner',
"DirectHours": 50000,
"EndDate": '3/31/2011',
"EngineeringHours": 4000,
"IndirectHours": 2000,
"Key": 1589,
"Number": '0',
"OtherHours": 300,
"ProductivityCurveType": 'BurnedEarned',
"ProjectManager": 'Doctor Who',
"ProjectName": 'Prisoner ZERO',
"StartDate": '5/1/2010'
}
This subsequent JSON sent to the server FAILS:
Once the user edits the form, the data is serialized client-side and sent BACK...where it (then) fails upon attempting to de-serialize the JSON.
{
"AppropriationAmount": 56300.00,
"AppropriationHours": 25000000.00,
"ArrThreshold": 11.00,
"ClientKey": ,
"Description": 'Find and incarcerate the escaped prisoner',
"DirectHours": 50000.00,
"EndDate": '3/31/2011',
"EngineeringHours": 4000.00,
"IndirectHours": 2000.00,
"Key": 1589,
"Number": '0',
"OtherHours": 300.00,
"ProductivityCurveType": 'BurnedEarned',
"ProjectManager": 'Doctor Who',
"ProjectName": 'Prisoner ZERO',
"StartDate": '5/1/2010'
}
This code throws the Error:
try
{
if (!String.IsNullOrEmpty(this.JSON))
{
serializer = new JavaScriptSerializer();
dialog = serializer.Deserialize<ProjectDecorator>(this.JSON);
}
}
catch (Exception ex)
{
// The message shows here
}
The Error thrown looks like:
{"Invalid JSON primitive: ."}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
ClientKey
不仅没有值,而且如果不将键和值放在双引号 (""
) 内,还会面临 JSON 有效性的风险。您的键没问题,但
string
值必须用双引号引起来。查看 JSON 网站,了解允许的内容和不允许的内容。Not only does
ClientKey
have no value, but you're risking JSON validness by not putting keys and values inside double quotations marks (""
).Your keys are OK, but
string
values must be surrounded by double quotes. Take a look at JSON website to see what's allowed and what's not."ClientKey": ,没有值
http://www.jsonlint.com/
"ClientKey": , has no value
http://www.jsonlint.com/