使用JSON.Net将JSON反序列化为DataTable

时间:2013-10-10 14:35:46

标签: c# json json.net

我正在尝试使用JSON.Net为.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)\/"}]}

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

方法1: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;

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

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

这两种方法都存在问题。

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

      

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

    堆栈跟踪如下:

      

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

我认为传递到跟踪顶部的JsonToken来电的GetColumnDataTypeStartObject

0 个答案:

没有答案