有效的JSON但无法反序列化

时间:2015-05-14 12:40:24

标签: c# json json.net

我想做点像......

Dictionary<string, string> values = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, string>>(jsonstring);

我有如下的有效数据,但它不能正确反序列化。有人能告诉我我做错了吗?

[
    {
        "Shop": "101",
        "Table": "hro",
        "ROWGuid": "031a0762-8d50-4d7c-9da9-9a233962733e",
        "JSONData": [
            {
                "BOXFER": false,
                "RO_NO": "0423684",
                "CUST_NO": "9902215",
                "VEH_NO": "  40174",
                "FLEET_NO": "",
                "BILLTO": "",
                "STATUS": "C",
                "RODATE": "2015-05-02T00:00:00",
                "TAX_PARTS": 26.94,
                "NTX_PARTS": 0,
                "TAX_LABOR": 16.92,
                "NTX_LABOR": 0,
                "TAX_SUBLET": 0,
                "NTX_SUBLET": 0,
                "DISCOUNTS": null,
                "TAX": 1.66,
                "SUPPLIES": 0.95,
                "CRD_CRG": 0,
                "CASH": 24.6,
                "CHECK": 0,
                "CREDIT_CRD": 0,
                "CHARGE": 0,
                "PAY_DATE": "2015-05-02T00:00:00",
                "TOTAL": 24.6,
                "DISP": 0,
                "DISL": 0,
                "DISS": 0,
                "DISPT": "Y",
                "DISLT": "Y",
                "DISST": "Y",
                "OK": null,
                "BAD": null,
                "RECOMENDS": null,
                "COMMENTS": null,
                "T_COST": 0,
                "P_COST": 7.89,
                "CHANGED": "0",
                "MILAGE": "117317",
                "ZIP": null,
                "CENTER": null,
                "CARD": 0,
                "ST": 0,
                "SOURCE": null,
                "BNAME": "BBS, DAVID/LIZ",
                "AC1": "734",
                "PHONE1": "877-3520",
                "YEAR": "1999",
                "MAKE": "LEXU",
                "MODEL": "ES 330",
                "LICENSE": "8Kff47",
                "PO_NUM": "",
                "CHECK_NO": "",
                "ADVANCE": 0,
                "POSTED": "Y",
                "WRITER": "006",
                "REOPENED": null,
                "SHIPTO_NO": "",
                "CreditCardNo": "",
                "CreditCardAuth": "",
                "CreditCardExp": null,
                "CheckAuth": "",
                "GSTTAX_PARTS": 0,
                "GSTTAX_LABOR": 0,
                "GSTTAX_SUBLET": 0,
                "GSTTAX": 0,
                "CONCERNS": "",
                "INSPECTIONS": "NNNNNN",
                "ARRIVED": "2015-05-02T13:30:00",
                "PROMISED": null,
                "CLOSED": "2015-05-02T17:35:14",
                "WAITING": false,
                "Hours": 0.68,
                "Coupon": null,
                "FleetDiscount": 0,
                "ORIG_NO": "0197494",
                "LockSupplies": false,
                "LockDiscounts": false,
                "DISPGST": " ",
                "DISLGST": " ",
                "DISSGST": " ",
                "TireTax": 0,
                "FET": 0,
                "TAXID": null,
                "FTInvoiceID": null,
                "FTPIN": null,
                "FTPaymentType": null,
                "ChangeWarningNeeded": 0,
                "DHPosted": false,
                "PRIMARYTECH": "391",
                "FTNotes": null,
                "FTLaborRate": 0,
                "Signature": null,
                "row_GUID": "031a0762-8d50-4d7c-9da9-9a2bf962733e",
                "VoidedByUser": false,
                "pdTaxableParts": -13.43,
                "pdNonTaxParts": 0,
                "pdTaxableLabor": -8.44,
                "pdNonTaxLabor": 0,
                "coupTaxableParts": 0,
                "coupNonTaxParts": 0,
                "coupTaxableLabor": 0,
                "coupNonTaxLabor": 0,
                "coupTaxableSublet": 0,
                "coupNonTaxSublet": 0,
                "MileageIn": "117317",
                "FLEETCARD": null,
                "FTAuthorization": null,
                "FTFleetID": null,
                "WorkOrder": null,
                "FTPosted": false,
                "FTTotalAuthorized": null,
                "OriginatorType": null,
                "OriginatorVersion": null,
                "PAYTYPE_GUID": null,
                "PAYTYPERELATION_GUID": null,
                "ePaySignatureId": null,
                "CREATED": "2015-05-02T13:30:09"
            }
        ],
        "Action": "add"
    },
    {
        "Shop": "101",
        "Table": "hro",
        "ROWGuid": "0dbbbd64-c85a-46a1-bf7d-df02d4467919",
        "JSONData": [
            {
                "BOXFER": false,
                "RO_NO": "0423656",
                "CUST_NO": "9904557",
                "VEH_NO": "  45152",
                "FLEET_NO": "",
                "BILLTO": "",
                "STATUS": "C",
                "RODATE": "2015-05-01T00:00:00",
                "TAX_PARTS": 30.93,
                "NTX_PARTS": 0,
                "TAX_LABOR": 28.2,
                "NTX_LABOR": 0,
                "TAX_SUBLET": 0,
                "NTX_SUBLET": 0,
                "DISCOUNTS": null,
                "TAX": 1.59,
                "SUPPLIES": 0,
                "CRD_CRG": 0,
                "CASH": 0,
                "CHECK": 0,
                "CREDIT_CRD": 23.58,
                "CHARGE": 0,
                "PAY_DATE": "2015-05-01T00:00:00",
                "TOTAL": 23.58,
                "DISP": 0,
                "DISL": 0,
                "DISS": 0,
                "DISPT": "Y",
                "DISLT": "Y",
                "DISST": "Y",
                "OK": null,
                "BAD": null,
                "RECOMENDS": null,
                "COMMENTS": null,
                "T_COST": 0,
                "P_COST": 9.24,
                "CHANGED": "0",
                "MILAGE": "115283",
                "ZIP": null,
                "CENTER": null,
                "CARD": 1,
                "ST": 0,
                "SOURCE": null,
                "BNAME": "SCIGE, RICHARD",
                "AC1": "734",
                "PHONE1": "847-6911",
                "YEAR": "1997",
                "MAKE": "FORT",
                "MODEL": "ESCAPE",
                "LICENSE": "6J8969",
                "PO_NUM": "",
                "CHECK_NO": "",
                "ADVANCE": 0,
                "POSTED": "Y",
                "WRITER": "006",
                "REOPENED": null,
                "SHIPTO_NO": "",
                "CreditCardNo": "7539",
                "CreditCardAuth": "07810z",
                "CreditCardExp": null,
                "CheckAuth": "",
                "GSTTAX_PARTS": 0,
                "GSTTAX_LABOR": 0,
                "GSTTAX_SUBLET": 0,
                "GSTTAX": 0,
                "CONCERNS": "oil change rotate air tires to 40",
                "INSPECTIONS": "NNNNNN",
                "ARRIVED": "2015-05-01T13:15:00",
                "PROMISED": null,
                "CLOSED": "2015-05-01T13:49:50",
                "WAITING": false,
                "Hours": 0.8,
                "Coupon": null,
                "FleetDiscount": 0,
                "ORIG_NO": "0197460",
                "LockSupplies": false,
                "LockDiscounts": false,
                "DISPGST": " ",
                "DISLGST": " ",
                "DISSGST": " ",
                "TireTax": 0,
                "FET": 0,
                "TAXID": null,
                "FTInvoiceID": null,
                "FTPIN": null,
                "FTPaymentType": null,
                "ChangeWarningNeeded": 0,
                "DHPosted": false,
                "PRIMARYTECH": "391",
                "FTNotes": null,
                "FTLaborRate": 0,
                "Signature": null,
                "row_GUID": "0dbaad64-c85a-46a1-bf7d-df02d5567919",
                "VoidedByUser": false,
                "pdTaxableParts": -19.43,
                "pdNonTaxParts": 0,
                "pdTaxableLabor": -17.71,
                "pdNonTaxLabor": 0,
                "coupTaxableParts": 0,
                "coupNonTaxParts": 0,
                "coupTaxableLabor": 0,
                "coupNonTaxLabor": 0,
                "coupTaxableSublet": 0,
                "coupNonTaxSublet": 0,
                "MileageIn": "115283",
                "FLEETCARD": null,
                "FTAuthorization": null,
                "FTFleetID": null,
                "WorkOrder": null,
                "FTPosted": false,
                "FTTotalAuthorized": null,
                "OriginatorType": null,
                "OriginatorVersion": null,
                "PAYTYPE_GUID": "a39d4372-a764-468c-bbcf-feae15fa9afd",
                "PAYTYPERELATION_GUID": "bb801921-c227-4bbd-80e4-49d7ea60fda0",
                "ePaySignatureId": null,
                "CREATED": "2015-05-01T13:15:51"
            }
        ],
        "Action": "add"
    }
]

3 个答案:

答案 0 :(得分:1)

反序列化为JArray或动态而非字典。

您还可以在此处生成自定义实体:http://json2csharp.com/

答案 1 :(得分:0)

您可以使用指定的类类型或使用动力学,并反序列化为该类型的列表:

var values = Newtonsoft.Json.JsonConvert.DeserializeObject<List<dynamic>>(myJson);

这里我使用了动态列表,如果您愿意,可以用特定的对象类型替换动态。然后,我可以迭代这些项目:

foreach (var item in values)
  {
      //do something
  }

答案 2 :(得分:0)

非常简单易行:

PSB:

var parsed = JsonConvert.DeserializeObject<JArray>(json);