本文介绍了JSON文件到Datatable错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 嗨专家, 我设法使用 HttpWebRequest 获取json文件。 我使用JSON.net将json文件转换为Datatable 。 尝试转换它时会出现以下错误: 意外的JSON令牌当阅读DataTable。预期的StartArray,得到了StartObject。路径' ',行 1 ,位置 1 。 这是我的c#代码: HttpWebRequest httpWebRequest =(HttpWebRequest)WebRequest.Create( http:// abc的.com / makes.json); httpWebRequest.Method = WebRequestMethods.Http.Get; httpWebRequest.Accept = application / json; charset = utf-8; string 文件; var response =(HttpWebResponse)httpWebRequest.GetResponse(); 使用( var sr = new StreamReader(response.GetResponseStream())) { file = sr.ReadToEnd(); } // var json = JObject.Parse(file); var table = JsonConvert.DeserializeObject< DataTable>(file); if (table.Rows.Count > 0 ) { // 做点什么 } 这里是文件的样子: { 使:[{ makeid: 21 , name: Acura},{ makeid: 250 , name: AM GENERAL},{ makeid: 5 , name: Audi}, { makeid: 239 , name: BMW},{ makeid: 65 , name: 别克},{ makeid: 66 , name: 凯迪拉克},{ makeid: 1 , name: Chevrolet},{ makeid: 10 , name: 克莱斯勒},{ makeid: 9 , 名称: 道奇},{ makeid: 2 , name: Ford},{ makeid: 11 , name : GMC},{ makeid: 13 , name: Honda},{ makeid: 117 , name: 悍马},{ makeid: 19 , name: Hyundai},{ makeid: 123 , name: 英菲尼迪}, { makeid: 240 , name: 五十铃},{ makeid: 242 , name: Jaguar},{ makeid: 130 , name: Jeep },{ makeid: 18 , name: Kia},{ makeid: 321 , name: Lada},{ makeid: 244 , name: Land Rover}, { makeid: 143 , name: Lexus},{ makeid: 144 , name : Lincoln},{ makeid: 15 , name: 马自达 },{ makeid: 245 , name: 梅赛德斯 - 奔驰},{ makeid: 152 , name: Mercury},{ makeid: 241 , name: 迷你},{ makeid: 158 , name: Mitsubishi},{ makeid : 14 , name: 日产},{ makeid: 167 , name: Oldsmobile},{ makeid: 281 , name: 欧宝},{ makeid: 180 , name: Plymouth},{ makeid: 181 , name: 庞蒂亚克},{ makeid: 17 , name : 保时捷},{ makeid: 311 , name: RAM},{ makeid: 331 , name: Renault},{ makeid: 238 , name: Saab},{ makeid: 12 , name: 土星},{ makeid: 243 , name: Scion},{ makeid: 202 , name: Subaru},{ makeid: 204 , name: 铃木},{ makeid: 209 , name: Toyota},{ makeid : 16 , name: Volkswagen},{ makeid: 6 , name: Volvo}, { makeid: 301 , name: VPG}]} 但是在提供这种格式时(手动)它工作! file = [{ \makeid \:21,\name \:\Acura \},{\makeid \:250,\name \:\AM GENERAL \},{\makeid \:5,\name \:\Audi \},{\makeid \:239,\name \\ \\:\BMW \},{\makeid \:65,\name \:\别克\},{\makeid \:66 ,\name \:\Cadillac \},{\makeid \:1,\name \:\Chevrolet \},{\ makeid \:10,\ name \:\Chrysler \},{\makeid \:9,\name \:\Dodge \},{\makeid \ :2,\name \:\Ford \},{\makeid \:11,\name \:\GMC \},{\\ \\makeid \:13,\name \:\Honda \},{\makeid \:117,\name \:\悍马\\},{\makeid \:19,\name \:\Hyundai \},{\makeid \:123,\name \ :\Infiniti \},{\makeid \:240,\name \:\Isuzu \},{\makeid \:242,\\ \\name \:\Jaguar \},{\makeid \:130,\name \:\Jeep \},{\makeid \\ \\:18,\name \:\Kia \ },{\makeid \:321,\name \:\Lada \},{\makeid \:244,\name \:\\ \\Land Rover \},{\makeid \:143,\name \:\Lexus \},{\makeid \:144,\ name\:\Lincoln \},{\makeid \:15,\name \:\Mazda \},{\makeid \ :245,\name \:\Mercedes-Benz \},{\makeid \:152,\name \:\Mercury \} ,{\makeid \:241,\name \:\Mini \},{\makeid \:158,\name \:\ Mitsubishi \},{\makeid \:14,\name \:\Nissan \},{\makeid \:167,\name \:\Oldsmobile \},{ \makeid \:281,\name \:\Opel \},{\makeid \:180,\name \:\普利茅斯\},{\makeid \:181,\name \:\Pontiac \},{\makeid \:17,\name \ :\Porsche \},{\makeid \:311,\name \:\RAM \},{\makeid \:331, \name \:\Renault \},{\makeid \:238,\name \:\Saab \},{\makeid \:12,\name \:\Saturn \},{\makeid \:243,\name \:\Scion \} ,{\makeid \:202,\name \:\Subaru \},{\makeid \:204,\name \:\ Suzuki \},{\makeid \: 209,\name \:\Toyota \},{\makeid \:16,\name \:\Volkswagen \},{\ makeid \:6,\name \:\Volvo \},{\makeid \:301,\name \:\VPG \ }]; 请告知我的代码有什么问题。我不能每次都手动完成。我需要从url读取json文件然后将它们转换为数据表以保存到DB。 感谢 Samira 解决方案 假设您收到的JSON看起来总是一样,请使用 DeserializeAnonymousType 方法 [ ^ ]: var table = JsonConvert .DeserializeAnonymousType(file, new {Makes = default (DataTable)})。Makes; Hi Experts,I managed to get json file using HttpWebRequest.And I used JSON.net to convert json file to Datatable.when trying to convert it give me the following error:Unexpected JSON token when reading DataTable. Expected StartArray, got StartObject. Path '', line 1, position 1.Here's my c# code:HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("http://abc.com/makes.json"); httpWebRequest.Method = WebRequestMethods.Http.Get; httpWebRequest.Accept = "application/json; charset=utf-8"; string file; var response=(HttpWebResponse)httpWebRequest.GetResponse(); using (var sr = new StreamReader(response.GetResponseStream())) { file = sr.ReadToEnd(); } //var json = JObject.Parse(file); var table = JsonConvert.DeserializeObject<DataTable>(file); if (table.Rows.Count > 0) { //do something }here is how the file looks like:{"makes":[{"makeid":21,"name":"Acura"},{"makeid":250,"name":"AM GENERAL"},{"makeid":5,"name":"Audi"},{"makeid":239,"name":"BMW"},{"makeid":65,"name":"Buick"},{"makeid":66,"name":"Cadillac"},{"makeid":1,"name":"Chevrolet"},{"makeid":10,"name":"Chrysler"},{"makeid":9,"name":"Dodge"},{"makeid":2,"name":"Ford"},{"makeid":11,"name":"GMC"},{"makeid":13,"name":"Honda"},{"makeid":117,"name":"Hummer"},{"makeid":19,"name":"Hyundai"},{"makeid":123,"name":"Infiniti"},{"makeid":240,"name":"Isuzu"},{"makeid":242,"name":"Jaguar"},{"makeid":130,"name":"Jeep"},{"makeid":18,"name":"Kia"},{"makeid":321,"name":"Lada"},{"makeid":244,"name":"Land Rover"},{"makeid":143,"name":"Lexus"},{"makeid":144,"name":"Lincoln"},{"makeid":15,"name":"Mazda"},{"makeid":245,"name":"Mercedes-Benz"},{"makeid":152,"name":"Mercury"},{"makeid":241,"name":"Mini"},{"makeid":158,"name":"Mitsubishi"},{"makeid":14,"name":"Nissan"},{"makeid":167,"name":"Oldsmobile"},{"makeid":281,"name":"Opel"},{"makeid":180,"name":"Plymouth"},{"makeid":181,"name":"Pontiac"},{"makeid":17,"name":"Porsche"},{"makeid":311,"name":"RAM"},{"makeid":331,"name":"Renault"},{"makeid":238,"name":"Saab"},{"makeid":12,"name":"Saturn"},{"makeid":243,"name":"Scion"},{"makeid":202,"name":"Subaru"},{"makeid":204,"name":"Suzuki"},{"makeid":209,"name":"Toyota"},{"makeid":16,"name":"Volkswagen"},{"makeid":6,"name":"Volvo"},{"makeid":301,"name":"VPG"}]}But when giving this format (manually) it works!file= "[ { \"makeid\": 21, \"name\": \"Acura\" }, { \"makeid\": 250, \"name\": \"AM GENERAL\" }, { \"makeid\": 5, \"name\": \"Audi\" }, { \"makeid\": 239, \"name\": \"BMW\" }, { \"makeid\": 65, \"name\": \"Buick\" }, { \"makeid\": 66, \"name\": \"Cadillac\" }, { \"makeid\": 1, \"name\": \"Chevrolet\" }, { \"makeid\": 10, \"name\": \"Chrysler\" }, { \"makeid\": 9, \"name\": \"Dodge\" }, { \"makeid\": 2, \"name\": \"Ford\" }, { \"makeid\": 11, \"name\": \"GMC\" }, { \"makeid\": 13, \"name\": \"Honda\" }, { \"makeid\": 117, \"name\": \"Hummer\" }, { \"makeid\": 19, \"name\": \"Hyundai\" }, { \"makeid\": 123, \"name\": \"Infiniti\" }, { \"makeid\": 240, \"name\": \"Isuzu\" }, { \"makeid\": 242, \"name\": \"Jaguar\" }, { \"makeid\": 130, \"name\": \"Jeep\" }, { \"makeid\": 18, \"name\": \"Kia\" }, { \"makeid\": 321, \"name\": \"Lada\" }, { \"makeid\": 244, \"name\": \"Land Rover\" }, { \"makeid\": 143, \"name\": \"Lexus\" }, { \"makeid\": 144, \"name\": \"Lincoln\" }, { \"makeid\": 15, \"name\": \"Mazda\" }, { \"makeid\": 245, \"name\": \"Mercedes-Benz\" }, { \"makeid\": 152, \"name\": \"Mercury\" }, { \"makeid\": 241, \"name\": \"Mini\" }, { \"makeid\": 158, \"name\": \"Mitsubishi\" }, { \"makeid\": 14, \"name\": \"Nissan\" }, { \"makeid\": 167, \"name\": \"Oldsmobile\" }, { \"makeid\": 281, \"name\": \"Opel\" }, { \"makeid\": 180, \"name\": \"Plymouth\" }, { \"makeid\": 181, \"name\": \"Pontiac\" }, { \"makeid\": 17, \"name\": \"Porsche\" }, { \"makeid\": 311, \"name\": \"RAM\" }, { \"makeid\": 331, \"name\": \"Renault\" }, { \"makeid\": 238, \"name\": \"Saab\" }, { \"makeid\": 12, \"name\": \"Saturn\" }, { \"makeid\": 243, \"name\": \"Scion\" }, { \"makeid\": 202, \"name\": \"Subaru\" }, { \"makeid\": 204, \"name\": \"Suzuki\" }, { \"makeid\": 209, \"name\": \"Toyota\" }, { \"makeid\": 16, \"name\": \"Volkswagen\" }, { \"makeid\": 6, \"name\": \"Volvo\" }, { \"makeid\": 301, \"name\": \"VPG\" } ]";please advise what's wrong with my code. I can't do it manually everytime. i need to read json files from url then convert them to datatables to be saved to DB.thanksSamira 解决方案 Assuming the JSON you're receiving always looks the same, use the DeserializeAnonymousType method[^]:var table = JsonConvert.DeserializeAnonymousType(file, new { Makes = default(DataTable) }).Makes; 这篇关于JSON文件到Datatable错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-30 06:39