C# 使用JSON.net时自动更改日期格式
我有一个JSON字符串,如下所示:C# 使用JSON.net时自动更改日期格式,c#,json,json.net,C#,Json,Json.net,我有一个JSON字符串,如下所示: { "id" : "f_372484", "vertical" : "furniture", "title" : "George NAKASHIMA Rocking Chair", "description" : "Walnut Rocking chair with bowed horizontal crestrail over spindle back. A great example of high quality workmanship
{
"id" : "f_372484",
"vertical" : "furniture",
"title" : "George NAKASHIMA Rocking Chair",
"description" : "Walnut Rocking chair with bowed horizontal crestrail over spindle back. A great example of high quality workmanship of George Nakashima (1905-1990).",
"dealerReference" : "",
"dibsReference" : "U1001058575397",
"dateAdded" : "2010-03-12T10:58:59.000-05:00",
"dateSold" : "2010-01-27T12:33:48.000-05:00",
"releaseDate" : "1969-12-31T19:00:00.000-05:00",
"modifiedDate" : "2014-02-08T22:41:15.000-05:00",
"uploadType" : "USER",
"status" : {
"productStatus" : "SOLD",
"contactForPrice" : "N",
"locked" : "N",
"posted" : "Y",
"newListing" : "N",
"inSaturdaySale" : "N",
"isAvailable" : "N",
"saveReleaseLater" : "N"
},
"dealerInternalNotes" : "",
"pickupAddress" : "same",
"overrideShipping" : "N",
"pieces" : 1,
"material" : {
"description" : "Carved Walnut",
"restricted" : "N"
},
"customMaterial" : {
"restricted" : "N",
"description" : "Carved Walnut"
},
"seller" : {
"id" : "f_8575",
"status" : "suspended"
},
"classification" : {
"style" : { },
"countryOfOrigin" : "USA",
"categories" : [ "A14" ],
"creationDate" : "Mid 20th C",
"inflatedCategories" : [ [ {
"key" : "f_",
"name" : "Furniture",
"label" : "furniture",
"code" : "FURNITURE",
"active" : "Y"
}, {
"key" : "f_A",
"name" : "Seating",
"label" : "seating",
"code" : "A",
"priority" : 1,
"active" : "Y",
"parent" : "f_"
}, {
"key" : "f_A14",
"name" : "Rocking Chairs",
"label" : "rocking-chairs",
"code" : "A14",
"priority" : 1,
"active" : "Y",
"parent" : "f_A"
} ] ],
"period" : "_1950"
},
"condition" : {
"description" : "Very Good. ",
"damageLosses" : "N",
"damageLight" : "N",
"damageSound" : "N",
"damageFading" : "N"
},
"images" : [ {
"position" : 1,
"status" : "DONE",
"thumb" : "/archivesD/upload/8575/397/dsc06990_1-02.jpg",
"small" : "/archivesD/upload/8575/397/dsc06990_1-01.jpg",
"medium" : "/archivesD/upload/8575/397/dsc06990_1-02.jpg",
"large" : "/archivesD/upload/8575/397/dsc06990_1.jpg"
}, {
"position" : 2,
"thumb" : "/archivesD/upload/8575/397/dsc06993-01.jpg",
"large" : "/archivesD/upload/8575/397/dsc06993.jpg"
}, {
"position" : 3,
"thumb" : "/archivesD/upload/8575/397/dsc06994-01.jpg",
"large" : "/archivesD/upload/8575/397/dsc06994.jpg"
}, {
"position" : 4,
"thumb" : "/archivesD/upload/8575/397/dsc06991-01.jpg",
"large" : "/archivesD/upload/8575/397/dsc06991.jpg"
}, {
"position" : 5,
"thumb" : "/archivesD/upload/8575/397/dsc06992-01.jpg",
"large" : "/archivesD/upload/8575/397/dsc06992.jpg"
}, {
"position" : 6,
"thumb" : "/archivesD/upload/8575/398/dsc06989-01.jpg",
"large" : "/archivesD/upload/8575/398/dsc06989.jpg"
}, {
"position" : 7
}, {
"position" : 8
}, {
"position" : 9
}, {
"position" : 10
} ],
"pricing" : {
"currency" : "USD",
"amount" : 6800.0,
"pricePerPiece" : "N",
"negotiable" : "Y",
"hidePrice" : "N",
"bargain" : "N",
"qualifyForBargain" : "N",
"initialPrice" : 6800,
"initialPriceCurrencies" : {
"USD" : 6800.00,
"PND" : 3944.00,
"EUR" : 4896.00
},
"amountCurrencies" : {
"USD" : 6800.00,
"PND" : 3944.00,
"EUR" : 4896.00
}
}
}
我从以下位置使用库:解析JSON并更新对象的属性
dynamic jsonObj = JObject.Parse(json);
dynamic furnitureItem = jsonObj.result.furnitureItem;
furnitureItem.title = "Hello";
furnitureItem.description = "World";
但是,在更新属性后,日期值会发生更改
furnitureItem.ToString();//I am using this line to get updated JSON
JSON:
我希望保持日期值不变不希望更改自动发生的时区。这是JSON.NET的文档页面,它向您展示了如何控制序列化日期的格式: 从文件中:
// default as of Json.NET 4.5
string isoJson = JsonConvert.SerializeObject(entry);
// {"Details":"Application started.","LogDate":"2009-02-15T00:00:00Z"}
JsonSerializerSettings microsoftDateFormatSettings = new JsonSerializerSettings
{
DateFormatHandling = DateFormatHandling.MicrosoftDateFormat
};
string microsoftJson = JsonConvert.SerializeObject(
entry, microsoftDateFormatSettings);
// {"Details":"Application started.","LogDate":"\/Date(1234656000000)\/"}
string javascriptJson = JsonConvert.SerializeObject(
entry, new JavaScriptDateTimeConverter());
// {"Details":"Application started.","LogDate":new Date(1234656000000)}
如果不打算处理日期,可以将其保留为字符串
JsonReader jsonReader = new JsonTextReader(new StringReader(json)) { DateParseHandling = DateParseHandling.None };
dynamic jsonObj = JObject.Load(jsonReader);
是的,JSON就是这样做的。约会很难玩。但是这篇文章可能会帮助你Hi@pptitle,谢谢你的回复,我没有在我的代码中的任何地方使用
反序列化
功能,类似的问题:
JsonReader jsonReader = new JsonTextReader(new StringReader(json)) { DateParseHandling = DateParseHandling.None };
dynamic jsonObj = JObject.Load(jsonReader);