I am trying to parse the json response that I get when I call the API to relax. The problem I am facing is that deserialization does not work every time, although I am making the same request. I do not know how to fix this, since try.catch does nothing better.
In addition, when I try to parse a very large answer (20+ json objects), the program never works.
I have a problem with googled myself, but I don't know a solution.
Unterminated string. Expected delimiter: "Path" drink [0] .strMeasure4 ', line 3, position 720.
- one of the mistakes that I get, it is never the same.
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);
edit:
I added a drink class:
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 strDrink, string strCategory, string strGlass, string strInstructions) { this.drinkId = drinkId; this.strDrink = strDrink; this.strCategory = strCategory; this.strGlass = strGlass; this.strInstructions = strInstructions; } } }
I tried:
http://www.thecocktaildb.com/api/json/v1/1/search.php?s=godfather
it went well 5 times, then i got this + json error i got. for the 6th time it was good.
http://pastebin.com/c0d29L0S (better format than insert below)
Unexpected end while deserializing an object. Path 'drinks [1] .strIngredient1', line 3, position 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":"JR Godfather", "strCategory":"Ordinary Drink", "strAlcoholic":"Alcoholic", "strGlass":"Old-fashioned glass", "strInstructions":"In an old-fashioned glass almost filled with ice cubes, combine both of the ingredients. Stir to mix the flavors.", "strDrinkThumb":null, "strIngredient1":
I understand why this is happening incorrectly right now, JSON is invalid, but this is the answer I received. Therefore, the code that I use to get the answer is incorrect. Right?
edit 3:
same request, good JSON response:
http://pastebin.com/e3WNxz0W
Now the program works, but it is incompatible.