gpt4 book ai didi

c# - NewtonSoft json转换器 "unterminated String, expected delimiter : "; ”

转载 作者:行者123 更新时间:2023-12-02 10:28:38 25 4
gpt4 key购买 nike

我正在尝试解析调用 REST API 时收到的 json 响应。我面临的问题是,即使我提出相同的请求,反序列化也并非每次都有效。我不知道如何修复它,因为 try.catch 并没有使任何事情变得更好。

此外,当我尝试解析一个非常大的响应(20+ json 对象)时,程序永远无法工作。

我自己用谷歌搜索了这个问题,但我不知道解决方案..

Unterminated string. Expected delimiter: ". Path 'drinks[0].strMeasure4', line 3, position 720.

是我遇到的错误之一,它永远不会相同。

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, 0, buffer.Length);
StringBuilder message = new StringBuilder();
int numberOfBytesRead = 0;
byte[] receiveBuffer = new byte[1024];

do
{
numberOfBytesRead = stream.Read(receiveBuffer, 0, receiveBuffer.Length);

message.AppendFormat("{0}", Encoding.ASCII.GetString(receiveBuffer, 0, numberOfBytesRead));

} while (stream.DataAvailable);


string response = message.ToString();

//Console.WriteLine("Response: \n" + response);
response = response.Substring(response.IndexOf("\r\n\r\n"));
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.1\r\n"
+ "Host: www.thecocktaildb.com\r\n\r\n";
}

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.strDrink, jsonData.strCategory, "", 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 strDrink, string strCategory, 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 (格式比下面的粘贴更好)

反序列化对象时意外结束。路径“drinks[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",
"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":

我明白为什么现在会出错,JSON 当然是无效的,但这是我收到的响应。所以我用来获取响应的代码是错误的......对吗?

编辑3:

相同的请求,良好的 JSON 响应:

http://pastebin.com/e3WNxz0W

现在程序可以运行,但不一致。

最佳答案

我猜实际的问题是您没有评估 HTTP 响应 header 。

结果很可能是分批发送的,即传输编码是“分块”的,但是天真的读者只会获得第一个 block 并使用它,而不是等待更多。这可能会在请求之间发生变化(例如,直接交付时分块,一旦缓存则不分块,反之亦然)。所以最后,不要重新发明轮子,只需使用 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 totransfer it as a series of chunks, each with its own size indicator,followed by an OPTIONAL trailer containing entity-header fields. Thisallows dynamically produced content to be transferred along with theinformation necessary for the recipient to verify that it hasreceived the full message.

<小时/>

当您遇到此类问题时,请尝试将代码拆分为更小的部分,并检查这些部分是否提供了预期的结果。

就您而言,您的 HTTP 下载显然不完整,因此您不能真正责怪 JSON 解析器吐出错误(因为它们是有效的)。

关于c# - NewtonSoft json转换器 "unterminated String, expected delimiter : "; ”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39436084/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com