c# – NewtonSoft json转换器“unterminated String,expected delimiter:”; “

前端之家收集整理的这篇文章主要介绍了c# – NewtonSoft json转换器“unterminated String,expected delimiter:”; “前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图解析调用rest API时得到的json响应.
我面临的问题是反序列化每次都不起作用,即使我发出相同的请求.我不知道如何修复它,因为try.catch没有做任何更好的事情.

此外,当我试图解析一个非常大的响应(20个json对象)时,程序永远不会工作.

我自己搜索了问题,但我不知道解决方案..

Unterminated string. Expected delimiter: “. Path ‘drinks[0].strMeasure4’,line 3,position 720.

是我得到的错误之一,它永远不会是一样的.

  1. using Newtonsoft.Json;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Net.Sockets;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using ConsoleApplication1;
  9.  
  10. namespace TCPclient
  11. {
  12. class Program
  13. {
  14. static void Main(string[] args)
  15. {
  16.  
  17. TcpClient client = new TcpClient();
  18.  
  19. client.Connect("www.thecocktaildb.com",80); // geen http
  20. string request = getRequestCoctail("margarita");
  21.  
  22.  
  23. NetworkStream stream = client.GetStream();
  24.  
  25.  
  26. byte[] buffer = Encoding.Default.GetBytes(request);
  27.  
  28. stream.Write(buffer,buffer.Length);
  29. StringBuilder message = new StringBuilder();
  30. int numberOfBytesRead = 0;
  31. byte[] receiveBuffer = new byte[1024];
  32.  
  33. do
  34. {
  35. numberOfBytesRead = stream.Read(receiveBuffer,receiveBuffer.Length);
  36.  
  37. message.AppendFormat("{0}",Encoding.ASCII.GetString(receiveBuffer,numberOfBytesRead));
  38.  
  39. } while (stream.DataAvailable);
  40.  
  41.  
  42. string response = message.ToString();
  43.  
  44. //Console.WriteLine("Response: \n" + response);
  45. response = response.Substring(response.IndexOf("\r\n\r\n"));
  46. try
  47. {
  48. dynamic jsonData = JsonConvert.DeserializeObject(response);
  49. List<Drink> drankjes = new List<Drink>();
  50.  
  51. for (int i = 0; i < jsonData.drinks.Count; i++)
  52. {
  53.  
  54. try
  55. {
  56. string id = jsonData.drinks[i].idDrink;
  57. string drink = jsonData.drinks[i].strDrink;
  58. string category = jsonData.drinks[i].strCategory;
  59. string instructions = jsonData.drinks[i].strInstructions;
  60. string glass = jsonData.drinks[i].strGlass;
  61. Console.WriteLine(glass);
  62. var d = new Drink(id,drink,category,instructions);
  63.  
  64. drankjes.Add(d);
  65. }
  66. catch (Exception)
  67. {
  68. Console.WriteLine("error");
  69. }
  70.  
  71. }
  72. }
  73. catch (Exception e)
  74. {
  75. Console.WriteLine(e.Message);
  76. }
  77.  
  78.  
  79. //Console.WriteLine(jsonData.drinks.Count);
  80.  
  81.  
  82. //Console.WriteLine(jsonData.drinks.Count); get ammount of drinks.
  83. Console.ReadKey();
  84.  
  85. }
  86. //www.thecocktaildb.com/api/json/v1/1/lookup.PHP?i=15679
  87. private static string getRequestCoctail(string coctail)
  88. {
  89.  
  90. ///api/json/v1/1/search.PHP?s=margarita
  91. return $"GET /api/json/v1/1/search.PHP?s=godfather HTTP/1.1\r\n"
  92. + "Host: www.thecocktaildb.com\r\n\r\n";
  93. }
  94.  
  95. private static string GetMetaDataCocktail(dynamic jsonData)
  96. {
  97.  
  98. dynamic drink = jsonData.drinks[0];
  99. return $"DrinkID : {drink.idDrink} \nDrinkName : {drink.strDrink} \nInstructions : {drink.strInstructions}";
  100. }
  101.  
  102. private static Drink GenerateNewDrink(dynamic jsonData)
  103. {
  104. Console.WriteLine(jsonData.idDrink,jsonData.strDrink,jsonData.strCategory,jsonData.strInstructions);
  105. return new Drink(jsonData.idDrink,"",jsonData.strInstructions);
  106.  
  107. }
  108. }
  109. }

编辑:

添加了饮料类:

  1. class Drink
  2. {
  3. public readonly string drinkId;
  4. public readonly string strDrink;
  5. public readonly string strCategory;
  6. public readonly string strInstructions;
  7. public readonly string strGlass;
  8.  
  9. public Drink(string drinkId,string strDrink,string strCategory,string strInstructions)
  10. {
  11. this.drinkId = drinkId;
  12. this.strDrink = strDrink;
  13. this.strCategory = strCategory;
  14. this.strInstructions = strInstructions;
  15. }
  16.  
  17. public Drink(string drinkId,string strGlass,string strInstructions)
  18. {
  19. this.drinkId = drinkId;
  20. this.strDrink = strDrink;
  21. this.strCategory = strCategory;
  22. this.strGlass = strGlass;
  23. this.strInstructions = strInstructions;
  24. }
  25. }
  26. }

我尝试过:

http://www.thecocktaildb.com/api/json/v1/1/search.php?s=godfather

它好了5次,然后我得到了这个错误我收到的json.
第6次也很好.

http://pastebin.com/c0d29L0S(更好的格式,然后下面的粘贴)

反序列化对象时意外结束.路径’饮料[1] .strIngredient1′,第3行,第1243位.

  1. {"drinks":[
  2. {"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
  3. },{"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:

3.6.1 Chunked Transfer Coding

The chunked encoding modifies the body of a message in order to
transfer it as a series of chunks,each with its own size indicator,
followed by an OPTIONAL trailer containing entity-header fields. This
allows dynamically produced content to be transferred along with the
information necessary for the recipient to verify that it has
received the full message.

当您遇到这样的问题时,请尝试将代码拆分为较小的部分,然后检查这些部分是否能够提供预期的结果.

在您的情况下,您的HTTP下载显然似乎不完整,因此您不能真正责怪JSON解析器吐出错误(因为它们是有效的).

猜你在找的C#相关文章