【问题标题】:Getting Unexpected JSON token when reading DataTable. Expected StartArray, got StartObject error读取 DataTable 时获取意外的 JSON 令牌。预期 StartArray,得到 StartObject 错误
【发布时间】:2020-04-22 17:29:22
【问题描述】:

尝试将 JSON 反序列化为数据集时,我在下面收到此错误。可能是什么问题呢?任何帮助表示赞赏。

错误:Newtonsoft.Json.JsonSerializationException:读取 DataTable 时出现意外的 JSON 令牌。预期 StartArray,得到 StartObject。路径“状态”,第 1 行,位置 11。

Dim ds As DataSet = JsonConvert.DeserializeObject(Of DataSet)(strJSON)

以下是 JSON 的示例: { "status": { "version": "1.0.0", "code": 0, "msg": "SuccessWithResult", "total": 10000, "page": 1, "pagesize": 10 }, "property": [ { "identifier": { "obPropId": 15096656312086, "fips": "12086", "apn": "0232340220100", "apnOrig": "232340220100", "attomId": 150966563 }, "lot": { "lotSize1": 0.249 }, "address": { "country": "US", "countrySubd": "FL", "line1": "2014 ALTON RD", "line2": "MIAMI BEACH, FL 33140", "locality": "Miami Beach", "matchCode": "ExaStr", "oneLine": "2014 ALTON RD, MIAMI BEACH, FL 33140", "postal1": "33140", "postal2": "4532", "postal3": "C007" }, "location": { "accuracy": "Street", "elevation": 0.0, "latitude": "25.796309", "longitude": "-80.140216", "distance": 0.0, "geoid": "CO12086, CS1292171, DB1200390, MT30002544, ND0000461513, ND0004455664, PL1245025, RS0000471909, ZI33140" }, "summary": { "propclass": "Single Family Residence / Townhouse", "propsubtype": "MISCELLANEOUS INDUSTRIAL", "proptype": "SFR", "yearbuilt": 1927, "propLandUse": "SFR", "propIndicator": "10" }, "building": { "size": { "universalsize": 4358 }, "rooms": { "bathstotal": 5.0, "beds": 6 } }, "vintage": { "lastModified": "2020-3-13", "pubDate": "2020-3-14" } }, { "identifier": { "obPropId": 862064312086, "fips": "12086", "apn": "0232340220080", "apnOrig": "232340220080", "attomId": 8620643 }, "lot": { "lotSize1": 0.2063 }, "address": { "country": "US", "countrySubd": "FL", "line1": "2008 ALTON RD", "line2": "MIAMI BEACH, FL 33140", "locality": "Miami Beach", "matchCode": "ExaStr", "oneLine": "2008 ALTON RD, MIAMI BEACH, FL 33140", "postal1": "33140", "postal2": "4532", "postal3": "C007" }, "location": { "accuracy": "Street", "elevation": 0.0, "latitude": "25.796222", "longitude": "-80.140290", "distance": 0.0, "geoid": "CO12086, CS1292171, DB1200390, MT30002544, ND0000461513, ND0004455664, PL1245025, RS0000471909, ZI33140" }, "summary": { "propclass": "Single Family Residence / Townhouse", "propsubtype": "MISCELLANEOUS INDUSTRIAL", "proptype": "SFR", "yearbuilt": 1920, "propLandUse": "SFR", "propIndicator": "10" }, "building": { "size": { "universalsize": 3037 }, "rooms": { "bathstotal": 3.0, "beds": 4 } }, "vintage": { "lastModified": "2020-3-13", "pubDate": "2020-3-14" } } ] }

【问题讨论】:

  • 能否请您添加您正在使用的语言/环境? (我想它是 VisualBasic,不是吗?)正确标记问题。谢谢。
  • 是的,对不起它的 vb.net。谢谢
  • 该错误表示您正在尝试反序列化为数组 (Expected StartArray),但输入的 JSON 字符串表示一个对象(它以 { 开头)。您需要先反序列化为一个对象,然后使用字段property(一个数组)来构建数据集。
  • 我对vb .net不熟悉所以我不知道你是怎么做的,但是网上应该有一些资源。

标签: asp.net json


【解决方案1】:

当您在结果的开头和结尾获得响应连接数组符号时,只需做一件简单的事情

dynamic result = await response.Content.ReadAsStringAsync();
dt = (DataTable)JsonConvert.DeserializeObject("["+result+"]", typeof(DataTable));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-09
    • 2021-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-09
    • 2020-02-17
    • 2017-08-29
    相关资源
    最近更新 更多