日期格式自动更改时,使用JSON.net

本文关键字:使用 JSON net 格式 日期 | 更新日期: 2023-09-27 18:06:56

我有一个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 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
    }
  }
}

我使用的库来自:http://james.newtonking.com/json
解析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:

{
  "id": "f_372484",
  "vertical": "furniture",
  "title": "Hello",
  "description": "World",
  "dealerReference": "",
  "dibsReference": "U1001058575397",
  "dateAdded": "2010-03-12T21:28:59+05:30",
  "dateSold": "2010-01-27T23:03:48+05:30",
  "releaseDate": "1970-01-01T05:30:00+05:30",
  "modifiedDate": "2014-02-08T22:41:15.000-05:00",
  "uploadType": "USER",
  "status": {
    "productStatus": "UNRELEASED",
    "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 Century",
    "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.0,
      "PND": 3944.0,
      "EUR": 4896.0
    },
    "amountCurrencies": {
      "USD": 6800.0,
      "PND": 3944.0,
      "EUR": 4896.0
    }
  }
}

我想保持日期值为
不要更改自动发生的时区

日期格式自动更改时,使用JSON.net

如果您不打算处理日期,您可以将它们保存为字符串。

JsonReader jsonReader = new JsonTextReader(new StringReader(json)) { DateParseHandling = DateParseHandling.None };
dynamic jsonObj = JObject.Load(jsonReader);

这是JSON的文档页面。. NET,它向您展示如何控制序列化日期的格式:http://james.newtonking.com/json/help/index.html?topic=html/DatesInJSON.htm

来自文档:

// 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)}