c# – NewtonSoft json转换器“unterminated String,expected de
我试图解析调用rest API时得到的json响应.
我面临的问题是反序列化每次都不起作用,即使我发出相同的请求.我不知道如何修复它,因为try.catch没有做任何更好的事情. 此外,当我试图解析一个非常大的响应(20个json对象)时,程序永远不会工作. 我自己搜索了问题,但我不知道解决方案..
是我得到的错误之一,它永远不会是一样的. using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; using System.Net.Sockets; using System.Text; using System.Threading.Tasks; using ConsoleApplication1; namespace TCPclient { class Program { static void Main(string[] args) { TcpClient client = new TcpClient(); client.Connect("www.thecocktaildb.com",80); // geen http string request = getRequestCoctail("margarita"); NetworkStream stream = client.GetStream(); byte[] buffer = Encoding.Default.GetBytes(request); stream.Write(buffer,buffer.Length); StringBuilder message = new StringBuilder(); int numberOfBytesRead = 0; byte[] receiveBuffer = new byte[1024]; do { numberOfBytesRead = stream.Read(receiveBuffer,receiveBuffer.Length); message.AppendFormat("{0}",Encoding.ASCII.GetString(receiveBuffer,numberOfBytesRead)); } while (stream.DataAvailable); string response = message.ToString(); //Console.WriteLine("Response: n" + response); response = response.Substring(response.IndexOf("rnrn")); try { dynamic jsonData = JsonConvert.DeserializeObject(response); List<Drink> drankjes = new List<Drink>(); for (int i = 0; i < jsonData.drinks.Count; i++) { try { string id = jsonData.drinks[i].idDrink; string drink = jsonData.drinks[i].strDrink; string category = jsonData.drinks[i].strCategory; string instructions = jsonData.drinks[i].strInstructions; string glass = jsonData.drinks[i].strGlass; Console.WriteLine(glass); var d = new Drink(id,drink,category,instructions); drankjes.Add(d); } catch (Exception) { Console.WriteLine("error"); } } } catch (Exception e) { Console.WriteLine(e.Message); } //Console.WriteLine(jsonData.drinks.Count); //Console.WriteLine(jsonData.drinks.Count); get ammount of drinks. Console.ReadKey(); } //www.thecocktaildb.com/api/json/v1/1/lookup.php?i=15679 private static string getRequestCoctail(string coctail) { ///api/json/v1/1/search.php?s=margarita return $"GET /api/json/v1/1/search.php?s=godfather HTTP/1.1rn" + "Host: www.thecocktaildb.comrnrn"; } private static string GetMetaDataCocktail(dynamic jsonData) { dynamic drink = jsonData.drinks[0]; return $"DrinkID : {drink.idDrink} nDrinkName : {drink.strDrink} nInstructions : {drink.strInstructions}"; } private static Drink GenerateNewDrink(dynamic jsonData) { Console.WriteLine(jsonData.idDrink,jsonData.strDrink,jsonData.strCategory,jsonData.strInstructions); return new Drink(jsonData.idDrink,"",jsonData.strInstructions); } } } 编辑: 我添加了饮料类: class Drink { public readonly string drinkId; public readonly string strDrink; public readonly string strCategory; public readonly string strInstructions; public readonly string strGlass; public Drink(string drinkId,string strDrink,string strCategory,string strInstructions) { this.drinkId = drinkId; this.strDrink = strDrink; this.strCategory = strCategory; this.strInstructions = strInstructions; } public Drink(string drinkId,string strGlass,string strInstructions) { this.drinkId = drinkId; this.strDrink = strDrink; this.strCategory = strCategory; this.strGlass = strGlass; this.strInstructions = strInstructions; } } } 我尝试过: http://www.thecocktaildb.com/api/json/v1/1/search.php?s=godfather 它好了5次,然后我得到了这个错误我收到的json. http://pastebin.com/c0d29L0S(更好的格式,然后下面的粘贴) 反序列化对象时意外结束.路径’饮料[1] .strIngredient1′,第3行,第1243位. {"drinks":[ {"idDrink":"11423","strDrink":"Godfather","strCategory":"Ordinary Drink","strAlcoholic":"Alcoholic","strGlass":"Old-fashioned glass","strInstructions":"Pour ingredients into an old-fashioned glass over ice and serve. (Bourbon may be substituted for scotch,if preferred.)","strDrinkThumb":null,"strIngredient1":"Scotch","strIngredient2":"Amaretto","strIngredient3":"","strIngredient4":"","strIngredient5":"","strIngredient6":"","strIngredient7":"","strIngredient8":"","strIngredient9":"","strIngredient10":"","strIngredient11":"","strIngredient12":"","strIngredient13":"","strIngredient14":"","strIngredient15":"","strMeasure1":"1 1/2 oz ","strMeasure2":"3/4 oz ","strMeasure3":" ","strMeasure4":" ","strMeasure5":" ","strMeasure6":" ","strMeasure7":" ","strMeasure8":"","strMeasure9":"","strMeasure10":"","strMeasure11":"","strMeasure12":"","strMeasure13":"","strMeasure14":"","strMeasure15":"","dateModified":null },{"idDrink":"11538","strDrink":"J. R.'s Godfather","strInstructions":"In an old-fashioned glass almost filled with ice cubes,combine both of the ingredients. Stir to mix the flavors.","strIngredient1": 我理解为什么它现在出错,JSON无效,但这是我收到的回复.所以我用来获得响应的代码是错的..对吧? 编辑3: 相同的请求,良好的JSON响应: http://pastebin.com/e3WNxz0W 现在该程序可以运行,但它是不存在的. 解决方法
我想实际问题是你没有评估HTTP响应头.
结果很可能是批量发送的,即转移编码是“分块”的,但是你天真的读者只能获得第一个块并使用它,而不是等待更多.这可能会在请求之间发生变化(例如,在直接传递时分块,在缓存后不分块,反之亦然).所以最后,不要重新发明轮子,只需使用WebClient. Read up the RFC section 3.6.1:
当您遇到这样的问题时,请尝试将代码拆分为较小的部分,然后检查这些部分是否能够提供预期的结果. 在您的情况下,您的HTTP下载显然似乎不完整,因此您不能真正责怪JSON解析器吐出错误(因为它们是有效的). (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |