我正在尝试反序列化此JSON对象,然后将其插入数据库:
{
"PresiderEmailAddress": "kyitss@gmail.com",
"MeetingNumber": "ME021616",
"MeetingName": " Quotation Meeting",
"MeetingDescription": "NULL",
"MeetingDate": "2\/16\/2016",
"MeetingVenue": "Consular Room",
"TimeStarted": "12:00:00",
"TimeEnded": "13:00:00",
"CompletionRate": 0.0,
"Remarks": "Actions still in progress"
}
但是我的WCF服务总是给我这个错误
无效的JavaScript属性标识符:}。路径'',第1行,第19位。
我在这里做错了什么?这是我的代码,顺便说一下:
public Int32 InsertMeeting(string jsonMeetingDetails)
{
MeetingDetailsForDeserializing obj = JsonConvert.DeserializeObject<MeetingDetailsForDeserializing>(jsonMeetingDetails);
using (SqlConnection connection = new SqlConnection(constring))
{
Int32 insertResult;
SqlCommand cmd = new SqlCommand("Insert into dbo.Meeting(MeetingNumber, PresiderEmailAddress, MeetingName, MeetingDescription, MeetingDate, MeetingVenue, TimeStarted, TimeEnded,CompletionRate, Remarks) values(@MeetingNumber, @PresiderEmailAddress,@MeetingName, @MeetingDescription, @MeetingDate, @MeetingVenue, @TimeStarted, @TimeEnded, @CompletionRate, @Remarks)");
cmd.CommandType = CommandType.Text;
cmd.Connection = connection;
cmd.Parameters.AddWithValue("@MeetingNumber", obj.jMeetingNumber);
cmd.Parameters.AddWithValue("@PresiderEmailAddress", obj.jPresiderEmailAddress);
cmd.Parameters.AddWithValue("@MeetingName", obj.jMeetingName);
cmd.Parameters.AddWithValue("@MeetingDescription", obj.jMeetingDescription);
cmd.Parameters.AddWithValue("@MeetingDate", obj.jMeetingDate);
cmd.Parameters.AddWithValue("@MeetingVenue", obj.jMeetingVenue);
cmd.Parameters.AddWithValue("@TimeStarted", obj.jTimeStarted);
cmd.Parameters.AddWithValue("@TimeEnded", obj.jTimeEnded);
cmd.Parameters.AddWithValue("@CompletionRate", obj.jCompletionRate);
cmd.Parameters.AddWithValue("@Remarks", obj.jRemarks);
connection.Open();
insertResult = cmd.ExecuteNonQuery();
connection.Close();
if (insertResult == 1)
{
return 1;
}
else
{
return 0;
}
}
}
修改 我昨晚正在寻找示例,似乎我用来反序列化对象的类与JSON不匹配。而且根据下面的帖子,我改变了它:
[DataContract]
public class MeetingDetailsForDeserializing
{
[DataMember(Order = 0)]
public string jPresiderEmailAddress { get; set; }
[DataMember(Order = 1)]
public string jMeetingNumber { get; set; }
[DataMember(Order = 2)]
public string jMeetingName { get; set; }
[DataMember(Order = 3)]
public string jMeetingDescription { get; set; }
[DataMember(Order = 4)]
public string jMeetingDate { get; set; }
[DataMember(Order = 5)]
public string jMeetingVenue { get; set; }
[DataMember(Order = 6)]
public string jTimeStarted { get; set; }
[DataMember(Order = 7)]
public string jTimeEnded { get; set; }
[DataMember(Order = 8)]
public decimal jCompletionRate { get; set; }
[DataMember(Order = 9)]
public string jRemarks { get; set; }
}
到此:
[DataContract]
public class MeetingDetailsForDeserializing
{
[DataMember(Order = 0)]
public string PresiderEmailAddress { get; set; }
[DataMember(Order = 1)]
public string MeetingNumber { get; set; }
[DataMember(Order = 2)]
public string MeetingName { get; set; }
[DataMember(Order = 3)]
public string MeetingDescription { get; set; }
[DataMember(Order = 4)]
public string MeetingDate { get; set; }
[DataMember(Order = 5)]
public string MeetingVenue { get; set; }
[DataMember(Order = 6)]
public string TimeStarted { get; set; }
[DataMember(Order = 7)]
public string TimeEnded { get; set; }
[DataMember(Order = 8)]
public decimal CompletionRate { get; set; }
[DataMember(Order = 9)]
public string Remarks { get; set; }
}
但我必须测试它,看看它是否有效。顺便说一句,我怎么能用Fiddler测试呢?
编辑好吧,当我在Fiddler上测试时,WCF给了我另一个错误:
解析值时遇到意外的字符:d。路径'',第0行,第0位。
我检查了JSON示例。没有办法会有未知的角色。
答案 0 :(得分:0)
在运行时没有看到内容的情况下很难分辨,但正如MaxJ提到的那样,它可能与MeetingDate中的转义字符有关,所以请尝试在“2016年2月16日”前面弹出一个@符号来否定它们。 / p>
答案 1 :(得分:0)
为什么用你的MeetingDetailsForDeserializing属性作为前缀j?你的json字符串应该与属性匹配。我试图模仿你拥有的同样的json,我能够成功地反序列化它。最初我期待日期错误,但这并没有发生。你能展示一下MeetingDetailsForDeserializing类吗?
然而,这是我试过的测试的快照。
JS模型
var data1 = {
"PresiderEmailAddress": "kyitss@gmail.com",
"MeetingNumber": "ME021616",
"MeetingName": " Quotation Meeting",
"MeetingDescription": "NULL",
"MeetingDate": "2\/16\/2016",
"MeetingVenue": "Consular Room",
"TimeStarted": "12:00:00",
"TimeEnded": "13:00:00",
"CompletionRate": 0.0,
"Remarks": "Actions still in progress"
};
&#13;
[这是c#模型类]
[DataContract]
public class MeetingDetailsForDeserializing
{
[DataMember(Order = 0)]
public string MeetingNumber { get; set; }
[DataMember(Order = 1)]
public string PresiderEmailAddress { get; set; }
[DataMember(Order = 2)]
public string MeetingName { get; set; }
[DataMember(Order = 3)]
public string MeetingDescription { get; set; }
[DataMember(Order = 4)]
public DateTime MeetingDate { get; set; }
[DataMember(Order = 5)]
public string MeetingVenue { get; set; }
[DataMember(Order = 6)]
public string TimeStarted { get; set; }
[DataMember(Order = 7)]
public string TimeEnded { get; set; }
[DataMember(Order = 8)]
public string CompletionRate { get; set; }
[DataMember(Order = 9)]
public string Remarks { get; set; }
}
如果我错过了什么,请告诉我。