简体   繁体   English

设置[PARAMETER]的值时出现意外结束-当在线验证者说字符串是无效的时,Visual Studio说字符串不是有效的JSON

[英]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的评论,我仍在考虑将其分解为多个结果集的可能性。

(C#) 为什么 Visual Studio 说它是 object 而 GetType 说它是 Func<object> ?<div id="text_translate"><p> C# 新手问题在这里。 以下代码(摘自 Apress Christian Gross 所著的《C# 从新手到专业人士》一书)报错:</p><pre> worksheet.Add("C3", CellFactories.DoAdd(worksheet["A2"], worksheet["B1"]));</pre><p> 原因是方法DoAdd()不接受给定的 arguments。</p><pre> public static Func<object> DoAdd(Func<object> cell1, Func<object> cell2) {...}</pre><p> VS 声称上面方法调用中的两个参数都是object类型,而该方法只接受Func<object> 。 但是两个工作表元素的<em>值</em>都是Func<object>类型:</p><pre> worksheet.Add("A2", CellFactories.Static(10.0));</pre><p> 这个Static方法只返回给定值:</p><pre> public static Func<object> Static(object value) { return () => value; } // return type= Func<object></pre><p> 当我将worksheet["A2"]转换为Func<object>时,代码确实有效。</p><p> 但是有一点我不明白。 object 实例的类型是Func<object> 。 我使用GetType()方法查看了这一点的证明,并将原始元素的 object 类型与强制转换的 object 类型(已接受)进行比较:</p><pre> Console.Writeline(worksheet["A2"].GetType()); // now cast to the correct type (why can't it do that implicitly, btw?) Funk1 = worksheet["A2"] as Func<object>; Console.Writeline(Funk1.GetType());</pre><p> .. 他们都是一样的! (类型 = System.Func'1[System.Object] )</p><p> 即使我使用.Equals()方法比较两种类型,它也会返回true 。</p><p> 然而,VS 在方法调用中将第一个 object 实例视为类型object 。 为什么? 为什么被调用的方法将参数“视为”为与 GetType() 返回的类型不同的类型? (如果是这样, GetType()方法有什么好处?)</p><p> 非常感谢您的建议/意见,(如果书中的示例给出错误并且您看不到原因,那么学习这门语言有点困难 - 因此,模糊的印象是GetType()或 VS 出了点问题.)</p></div></object> - (C#) why does Visual Studio say it's an object while GetType says it's a Func<object>?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 (C#) 为什么 Visual Studio 说它是 object 而 GetType 说它是 Func<object> ?<div id="text_translate"><p> C# 新手问题在这里。 以下代码(摘自 Apress Christian Gross 所著的《C# 从新手到专业人士》一书)报错:</p><pre> worksheet.Add("C3", CellFactories.DoAdd(worksheet["A2"], worksheet["B1"]));</pre><p> 原因是方法DoAdd()不接受给定的 arguments。</p><pre> public static Func<object> DoAdd(Func<object> cell1, Func<object> cell2) {...}</pre><p> VS 声称上面方法调用中的两个参数都是object类型,而该方法只接受Func<object> 。 但是两个工作表元素的<em>值</em>都是Func<object>类型:</p><pre> worksheet.Add("A2", CellFactories.Static(10.0));</pre><p> 这个Static方法只返回给定值:</p><pre> public static Func<object> Static(object value) { return () => value; } // return type= Func<object></pre><p> 当我将worksheet["A2"]转换为Func<object>时,代码确实有效。</p><p> 但是有一点我不明白。 object 实例的类型是Func<object> 。 我使用GetType()方法查看了这一点的证明,并将原始元素的 object 类型与强制转换的 object 类型(已接受)进行比较:</p><pre> Console.Writeline(worksheet["A2"].GetType()); // now cast to the correct type (why can't it do that implicitly, btw?) Funk1 = worksheet["A2"] as Func<object>; Console.Writeline(Funk1.GetType());</pre><p> .. 他们都是一样的! (类型 = System.Func'1[System.Object] )</p><p> 即使我使用.Equals()方法比较两种类型,它也会返回true 。</p><p> 然而,VS 在方法调用中将第一个 object 实例视为类型object 。 为什么? 为什么被调用的方法将参数“视为”为与 GetType() 返回的类型不同的类型? (如果是这样, GetType()方法有什么好处?)</p><p> 非常感谢您的建议/意见,(如果书中的示例给出错误并且您看不到原因,那么学习这门语言有点困难 - 因此,模糊的印象是GetType()或 VS 出了点问题.)</p></div></object> - (C#) why does Visual Studio say it's an object while GetType says it's a Func<object>? 发布JSON数据时,MVC控制器的string []参数为null - MVC controller's string[] parameter is null when posting JSON data 反序列化对字典的Json响应 <string, object> 引发错误:反序列化对象时意外结束 - Deserializing Json Response to a Dictionary<string, object> throws error : unexpected end when deserializing object 当Visual Studio仅允许将其作为“应用程序”设置时,如何允许用户更改SQL连接字符串? - How can I allow users to alter the SQL connection string, when Visual Studio only allows it to be an “Application” setting? 解析 json 字符串时出现意外字符 - unexpected char when parsing json string Visual Studio:[DebuggerDisplay],在指定格式时显示“不是有效的格式说明符” - Visual Studio: [DebuggerDisplay], shows "not a valid format specifier" when specifying format c# substring 设置结束参数时方法错误 - c# substring method error when setting end parameter 调试时,Visual Studio为什么说中位数是未分配的变量? -Answered - When debugging why does visual studio say median is an unassigned variable? -Answered 当我尝试输出值时,它显示为System.String [] - It says System.String[] when i tries to output the value 编辑 C# 文件时,Visual Studio 的“启用所有 JSON 编辑器功能”的文档在哪里? - Where's the documentation for "Enable All JSON editor features" for Visual Studio, when editing a C# file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM