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

时间:2014-06-05 11:51:41

标签: 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 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
    }
  }
}

我想保留日期值,因为它是 不想改变自动发生的时区。

2 个答案:

答案 0 :(得分:1)

如果您不打算使用日期,则可以将其保留为字符串。

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

答案 1 :(得分:0)

这里是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)}