![](/img/trans.png)
[英]json newtonsoft : Deserialize Object containing a list of string
[英]deserialize json string containing date
我一直在關於如何在json字符串中反序列化日期的一些帖子。
帖子提到對字符串進行替換以重新格式化Date部分。
json字符串看起來像這樣:
"/Date(1336258800000)/\"
顯然我需要達到的目的是:
"new Date(1336258800000)"
問題是我嘗試和替換,或indexOf')/ \\'它沒有找到要替換的字符串(indexOf是-1)
任何人都可以看到我做錯了什么?
JavaScriptSerializer jss = new JavaScriptSerializer();
//Fix an issue with Json Dates
string json = eventArgument.Replace( @"/Date(", "new Date(" ).Replace( @")/\", ")" );
int index = json.IndexOf( @")/\", 0 );
WorkPattern wp = jss.DeserializeObject( json ) as WorkPattern;
這是完整的json字符串:
"{\"WorkPatternDays\":[{\"WorkPatternDayShifts\":[{\"WorkPatternDayShiftRates\":[{\"Duration\":8.5,\"Sequence\":0,\"WorkPatternDayShiftID\":186,\"WorkPatternDayShiftRateID\":105,\"Deleted\":false,\"UpdatedUser\":\"\",\"UpdatedDate\":\"/Date(1336258800000)/\"}],\"WorkPatternDayShiftBreaks\":[{\"PaidBreak\":true,\"Duration\":1,\"EndTime\":\"/Date(1336050000000)/\",\"StartTime\":\"/Date(1336046400000)/\",\"WorkPatternDayShiftID\":186,\"WorkPatternDayShiftBreakID\":284,\"Deleted\":false,\"UpdatedUser\":\"\",\"UpdatedDate\":\"/Date(1336258800000)/\"},{\"PaidBreak\":false,\"Duration\":0.25,\"EndTime\":\"/Date(1336058100000)/\",\"StartTime\":\"/Date(1336057200000)/\",\"WorkPatternDayShiftID\":186,\"WorkPatternDayShiftBreakID\":285,\"Deleted\":false,\"UpdatedUser\":\"\",\"UpdatedDate\":\"/Date(1336258800000)/\"}],\"Duration\":8.5,\"EndTime\":\"/Date(1336062600000)/\",\"StartTime\":\"/Date(1336032000000)/\",\"WorkPatternDayID\":186,\"WorkPatternDayShiftID\":186,\"Deleted\":false,\"UpdatedUser\":\"\",\"UpdatedDate\":\"/Date(1336258800000)/\"}],\"DayOfWeek\":1,\"DayOfWeekNumber\":1,\"WorkPatternID\":105,\"WorkPatternDayID\":186,\"Deleted\":false,\"UpdatedUser\":\"\",\"UpdatedDate\":\"/Date(1336258800000)/\"},{\"WorkPatternDayShifts\":[{\"WorkPatternDayShiftRates\":[],\"WorkPatternDayShiftBreaks\":[{\"PaidBreak\":true,\"Duration\":0.5,\"EndTime\":\"/Date(1336041000000)/\",\"StartTime\":\"/Date(1336039200000)/\",\"WorkPatternDayShiftID\":187,\"WorkPatternDayShiftBreakID\":286,\"Deleted\":false,\"UpdatedUser\":\"\",\"UpdatedDate\":\"/Date(1336258800000)/\"}],\"Duration\":5.5,\"EndTime\":\"/Date(1336046400000)/\",\"StartTime\":\"/Date(1336026600000)/\",\"WorkPatternDayID\":187,\"WorkPatternDayShiftID\":187,\"Deleted\":false,\"UpdatedUser\":\"\",\"UpdatedDate\":\"/Date(1336258800000)/\"}],\"DayOfWeek\":3,\"DayOfWeekNumber\":3,\"WorkPatternID\":105,\"WorkPatternDayID\":187,\"Deleted\":false,\"UpdatedUser\":\"\",\"UpdatedDate\":\"/Date(1336258800000)/\"}],\"WorkPatternName\":\"Naths Test Work Pattern\",\"WorkPatternID\":105,\"Deleted\":false,\"UpdatedUser\":\"\",\"UpdatedDate\":\"/Date(1336258800000)/\"}"
更多信息,看看它們如何組合在一起:
代碼背后:
public override void DataBind()
{
try
{
if ( this.WorkPattern != null )
{
//Create a javascript serializer
JavaScriptSerializer jss = new JavaScriptSerializer();
//Get the serialised object as a json string
string json = jss.Serialize( this.WorkPattern );
//Run the jquery code
base.RunjQueryCode(
String.Format( "loadWorkPattern({0});", json ) );
jss = null;
}
}
catch ( Exception )
{
throw;
}
}
protected override void HandlePostbacks( string eventTarget, string eventArgument )
{
try
{
switch ( eventTarget )
{
case "Save":
JavaScriptSerializer jss = new JavaScriptSerializer();
//Fix an issue with Json Dates
string json = eventArgument.Replace( @"/Date(", "new Date(" ).Replace( @")/\", ")" );
int index = json.IndexOf( @")/\\", 0 );
WorkPattern wp = jss.DeserializeObject( json ) as WorkPattern;
object o = jss.Deserialize<WorkPattern>( json );
break;
default: break;
}
base.HandlePostbacks( eventTarget, eventArgument );
}
catch ( Exception )
{
throw;
}
}
標記/ js:
function loadWorkPattern(jsonData) {
//Store the work pattern
_workPattern = jsonData;
//Loop through the work pattern days
$.each(_workPattern.WorkPatternDays, function (key, workPatternDay) {
//Loop through each shift
$.each(workPatternDay.WorkPatternDayShifts, function (key, workPatternShift) {
addShift(workPatternShift, workPatternDay.DayOfWeekNumber);
//Loop through each break
$.each(workPatternShift.WorkPatternDayShiftBreaks, function (key, workPatternDayShiftBreak) {
addBreak(workPatternDayShiftBreak);
});
});
});
}
function saveWorkPattern() {
__doPostBack('Save', JSON.stringify(_workPattern));
}
我在發送回服務器之前調用JSON.stringify來序列化存儲對象的序列化,這是我做錯了什么?
UPDATE
這是工作代碼:
string json = eventArgument.Replace( @"/Date(", "\\/Date(" ).Replace( @")/", ")\\/" );
嘗試int index = json.IndexOf( @")/\\\\", 0 );
- 在斜線前加上另一個斜線
更新
JavaScriptSerializer s = new JavaScriptSerializer();
string date = s.Serialize(DateTime.Now);
int index = date.IndexOf(@")\/", 0);
Console.WriteLine(index); // index = 21
更新 - 解決方案
問題是初始的字符串/Date(1336258800000)/
,而不是/Date(1336258800000)/\\
作為最后一個斜線是逸出"
的JSON字符。而對於desiarization格式應dirrerent,所以工作的解決方案是
string json = eventArgument.Replace( @"/Date(", "\\/Date(" ).Replace( @")/", ")\\/" );
我使用正則表達式,希望這不是問題。 正則表達式檢測/Date(NUMBER)/\\
並在正則表達式匹配中將NUMBER作為一組獲取,因此我使用它來替換dateTimeJson中與其構造函數中指定的正則表達式匹配的新日期(NUMBER)中的所有內容。
//the source JSON
string dateTimeJson = "/Date(1336258800000)/\\";
string result = "";
//you might want to do a quick IndexOf("Date") to make sure that there is a date
//so you won't trouble yourselve with regex computation unnecessarily. performance?
//find Date(NUMBER) matches and get the NUMBER then use it again with new Date in the
//call to replace
System.Text.RegularExpressions.MatchCollection matches = null;
System.Text.RegularExpressions.Regex regex = null;
try
{
//at the end is one forwared slash for regex escaping \ and another is the \ that is escaped
regex = new System.Text.RegularExpressions.Regex(@"/Date\(([0-9]*)\)/\\");
matches = regex.Matches(dateTimeJson);
string dateNumber = matches[0].Groups[1].Value;
result = regex.Replace(dateTimeJson, "new Date(" + dateNumber + ")");
}
catch (Exception iii)
{
//MessageBox.Show(iii.ToString());
}
MessageBox.Show(result);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.