[英]Unexpected end when setting [PARAMETER]'s Value - Visual Studio says string is not valid JSON when online validators say it is
I have a SQL stored procedure returning a very large complex JSON string as its result through the use of FOR JSON PATH
and a set of JSON_QUERY
, and am attempting to convert this String to an object using JsonConvert.DeserializeObject<MyObject>
in a custom TypeHandler I've written for Dapper. 我有一个SQL存储过程通过使用FOR JSON PATH
和一组JSON_QUERY
返回一个非常大的复杂JSON字符串作为结果,并且正在尝试在自定义JsonConvert.DeserializeObject<MyObject>
使用JsonConvert.DeserializeObject<MyObject>
将此字符串转换为对象。我为Dapper写过书。 However I'm running into an issue where I get the following error: 但是,我遇到了以下错误:
Unexpected end when setting ChargeType's value. Path...
My object is expecting an int for that value, and the JSON is providing it with a proper value type. 我的对象期望该值是int,而JSON为它提供了正确的值类型。 Additionally, when I attempt to inspect the string in Visual Studio it says that it is not formatted as a JSON string. 此外,当我尝试在Visual Studio中检查字符串时,它表示未将其格式化为JSON字符串。 Using an online JSON validator, however, (jsonformatter.org) I'm told that the string IS valid JSON. 但是,使用在线JSON验证器(jsonformatter.org),我被告知该字符串是有效的JSON。
Here is a small sample section of the JSON that is similar to what is being deserialized. 这是JSON的一小部分示例,与正在反序列化的示例相似。 The important thing here is that the JSON string is extremely large, with many more entries like this one. 这里重要的是,JSON字符串非常大,其中包含更多这样的条目。 This entry in particular is an invoice, and belongs to an invoice header with many other invoices. 该条目尤其是发票,并且与许多其他发票一起属于发票抬头。 The invoice header belongs to an invoice group which has other invoice headers in it as well, and the invoice group belongs to an order which has several other invoice groups. 发票抬头属于其中也具有其他发票抬头的发票组,并且发票组属于具有其他几个发票组的订单。
...More objects and parent object above... ...更多对象和上方的父对象...
{
"Id": "B5F18C84-4790-E811-80CC-005056BA0972",
"RefId": "4532516",
"InboundWeight": 0,
"OutboundWeight": 4352,
"Total": 4.75,
"Surcharge": 0,
"FlatRate": 130,
"Credit": 0,
"Surcharge2": 0,
"Cwt": 0,
"ChargeType": 155,
"Key": "18981760",
"ReferenceKey": "3003858587",
"StopKey": "11792108",
"RawTotal": 4.75,
"ChargetTypeKey": "SANITIZED",
"ChargeTypeDescription": "SANITIZED",
"RawWeight": 4352,
"Header_Id": "B4F18C84-4790-E811-80CC-005056BA0972"
}
...More objects below... ...下面有更多物体...
It turns out that Visual Studio is indeed doing something wrong on its end. 事实证明,Visual Studio确实确实在做错事。 While the stored procedure returns the entire JSON string, Visual Studio truncates the string extremely short. 当存储过程返回整个JSON字符串时,Visual Studio会将字符串截短。 I'm currently looking into options for either preventing it from truncating the string or splitting out the JSON into multiple result sets. 我目前正在寻找防止其截断字符串或将JSON分成多个结果集的选项。 I'd like to avoid this if possible as this will require spinning through the JSON in order to add invoice groups to the order, headers to their groups, invoices to their proper headers, etc. 我想尽可能避免这种情况,因为这需要遍历JSON才能将发票组添加到订单,将标题添加到其组,将发票添加到正确的标题等。
Edit: Further clarification and an actual solution 编辑:进一步澄清和实际解决方案
My issue was partially related to the way SQL Server and ADO.net communicate from my initial reading on the subject. 我的问题部分与我最初阅读该主题时有关SQL Server和ADO.net的通信方式有关。 Essentially, the data was being sent over in truncated chunks, and because I was using Dapper's QuerySingle<MyObject>
to attempt to get all of it at once, I was actually only accessing the first chunk which resulted in the error being thrown. 本质上,数据是以截断的块形式发送的,并且由于我正在使用Dapper的QuerySingle<MyObject>
尝试一次获取所有数据,因此实际上我仅访问第一个块,从而导致抛出错误。
Using String.Concat(Query<string>(MyArgsHere))
I am able to get the entire JSON string, and then from there I should be able to serialize that into my object. 使用String.Concat(Query<string>(MyArgsHere))
我可以获取整个JSON字符串,然后从那里我应该可以将其序列化到我的对象中。 I am, however, still looking at potentially breaking this down into multiple result sets as per @JeroenMostert's comment. 但是,根据@JeroenMostert的评论,我仍在考虑将其分解为多个结果集的可能性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.