1

我正在尝试使用 JSON.Net for .Net 3.5 将 JSON 有效负载反序列化为数据表。我的 JSON 字符串格式如下:

{"d":[{"__metadata":{"id":"http://testingurl.mydomain.net/Mysvc.svc/AccountingDate(datetime'2013-08-02T00%3A00%3A00')","uri":"http://testingurl.mydomain.net/Mysvc/AccountingDate(datetime'2013-08-02T00%3A00%3A00')","type":"AccountingDate"},"ACCOUNTING_DT":"\/Date(1375401600000)\/","DTTM_STAMP":"\/Date(1374278400000)\/","PRE_ACCOUNTING_DT":"\/Date(1374883200000)\/","FISCAL_YEAR":"2014","ACCOUNTING_PERIOD":"6","FYR_BEGIN_DT":"\/Date(1372464000000)\/","FYR_END_DT":"\/Date(1403827200000)\/","FMN_BEGIN_DT":"\/Date(1375488000000)\/","FMN_END_DT":"\/Date(1377820800000)\/","FISCAL_MONTH":"2","FISCAL_WEEK":"F","WK_BEGIN_DT":"\/Date(1374969600000)\/","WK_END_DT":"\/Date(1375488000000)\/","CALENDAR_ID":"WE","SETID":"EYLLP","COMPANY_DESCR":"Ernst & Young LLP","PRIOR_FYR_END_DT":"\/Date(1372377600000)\/","PRIOR_FYR_TODATE":"\/Date(1343347200000)\/","CAL_MONTH":"8","IW_CREATE_DT":"\/Date(1121385600000)\/","IW_UPDATE_DT":"\/Date(1374883200000)\/","INSTANCE_ID":"PA01","PMTD_BEGIN_DT":"\/Date(1372464000000)\/","PMTD_END_DT":"\/Date(1375401600000)\/","MTD12_BEGIN_DT":"\/Date(1343433600000)\/","MTD12_END_DT":"\/Date(1374796800000)\/","PRIOR_FYR_BEGIN_DT":"\/Date(1341014400000)\/","CALENDAR_MONTH_NAME":"August","CALENDAR_YEAR":"2013","PRY_FMN_BEGIN_DT":"\/Date(1341014400000)\/","PRY_FMN_END_DT":"\/Date(1343952000000)\/"}]}

我已经用多个在线解析器验证了这个字符串,他们说它是有效的。我尝试使用以下方法进行反序列化。

方法一:jsonResponse等于上面的字符串

JSONNet.JsonSerializer json = new JSONNet.JsonSerializer();
json.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
json.ObjectCreationHandling = Newtonsoft.Json.ObjectCreationHandling.Replace;
json.MissingMemberHandling = Newtonsoft.Json.MissingMemberHandling.Ignore;
json.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
StringReader sr = new StringReader(jsonResponse);
JSONNet.JsonTextReader treader = new JSONNet.JsonTextReader(sr);
DataTable dt = json.Deserialize(treader, (typeof(DataTable))) as DataTable;

方法二:jsonResponse等于上面的字符串

DataSet ds = JSONNet.JsonConvert.DeserializeObject<DataSet>(jsonResponse);

两种方法都有问题。

  • 方法 1 返回没有列或行的数据表
  • 方法2抛出错误:

    指定的参数超出了有效值的范围。

    堆栈跟踪如下:

    在 Newtonsoft.Json.Converters.DataTableConverter.GetColumnDataType(JsonToken tokenType)
    在 Newtonsoft.Json.Converters.DataTableConverter.ReadJson(JsonReader reader, Type objectType, Object existingValue, JsonSerializer serializer)
    在 Newtonsoft.Json.Converters.DataSetConverter.ReadJson(JsonReader reader ,在 Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(
    JsonReader
    reader, Type objectType,
    Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType) 处的布尔 checkAdditionalContent )
    在 Newtonsoft.Json.JsonSerializer.Deserialize(JsonReader reader, Type objectType)
    at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
    at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings ) )
    在 Newtonsoft.Json.JsonConvert.DeserializeObject[T](字符串值)

我相信在跟踪顶部JsonToken传递给调用的是.GetColumnDataTypeStartObject

4

0 回答 0