为什么我会收到带有此代码的JsonReaderException?

编程入门 行业动态 更新时间:2024-10-26 20:21:04
本文介绍了为什么我会收到带有此代码的JsonReaderException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有以下代码尝试打开.json文件并阅读:

I've got this code to try to open a .json file and read it:

[Route("{unit}/{begindate}")] public string Get(string unit, string begindate) { string _unit = unit; string _begindate = String.Format("{0}01", PlatypusWebReportsConstsAndUtils.HyphenizeYYYYMM(begindate)); string appDataFolder = HttpContext.Current.Server.MapPath("~/App_Data/"); // semi-hardcoded file name for now string jsonFilename = string.Format("PoisonToe_{0}_{1}.json", _unit, _begindate); string fullPath = Path.Combine(appDataFolder, jsonFilename); JObject platypusComplianceJson = JObject.Parse(File.ReadAllText(fullPath)); . . .

在最后一行,我得到:

Newtonsoft.Json.JsonReaderException HResult = -2146233088 Message =从JsonReader读取JObject时出错.当前的JsonReader项不是对象:

Newtonsoft.Json.JsonReaderException was unhandled by user code HResult=-2146233088 Message=Error reading JObject from JsonReader. Current JsonReader item is not an object:

根据我在此处所读内容,做到的方式.我在做什么错了?

Based on what I read here, I thought this would be the way to do it. What am I doing wrong?

推荐答案

如果没有注释中要求的信息,很难给出更好的答案,但这意味着,正如它所暗示的那样,当前令牌不是开头的.对象.

It's difficult to give a better answer without the information requested in comments, but this means, as it suggests, that the current token isn't the start of an object.

请清楚一点,我们在JSON词汇中谈论的是对象",而在C#/OOP词汇意义上是谈论的"不是".

Just to be clear, we're talking about "object" in the JSON vocabulary, and not in the C#/OOP vocabulary sense.

听起来这里发生的事情类似于非JSON bool.Parse("1").是的,"1"是解析方法(例如,int.Parse)的有效输入,但这是错误的方法.

It sounds like what's going on here is something like the non-JSON bool.Parse("1"). Yes, "1" is valid input to a parse method (int.Parse, for instance), but this is the wrong method.

一个简单的技巧(警告:创可贴修复)是切换到JToken.Parse. JToken是JObject,JArray,JValue和批次的多态父代,因此它的解析能够处理更多类型的非结构化输入.

The simple trick (warning: Band-Aid fix) is to switch to JToken.Parse. JToken is a polymorphic parent of JObject, JArray, JValue, and the lot, so its parse is able to handle a lot more types of unstructured input.

在您确定之前,您一定要仔细检查合同和文件,以确认哪个是错误的,然后再确定.成功地解析然后用作JObject的JArray肯定没有好处.

Before you do that, as I'm sure you know, you should of course double-check your contracts and the file, to see which is wrong. There's certainly no good in successfully parsing a JArray that you then use as a JObject.

出于良好的考虑,一些可能导致此错误的文件文本为:

For good measure, a few file texts that would cause this error are:

  • (空文件)
  • [{ "test": "val" }](数组)
  • "test"(字符串)
  • null
  • (empty file)
  • [{ "test": "val" }] (array)
  • "test" (string)
  • null

这些都应该是对JToken.Parse的有效输入,但是由于它们不是对象,因此它们都会在JObject.Parse上给出此错误.您需要类似的东西:

These should all be valid inputs to JToken.Parse, I believe, but they'd all give this error on JObject.Parse, because they aren't objects. You need something like:

  • { "test": "val" }
  • { "test": { "val": 2 } }
  • { "test": "val" }
  • { "test": { "val": 2 } }

或者类似的东西.

回到我的OOP点,JObject不是JSON中所有内容的基本基本类型,但是JToken是.因此,即使您可以说,

Back on my OOP point, a JObject isn't the elementary base type of everything in JSON, but JToken is. So even though you could say,

object i = new int[0];

在C#中,你不能说

JObject i = JObject.Parse("[0, 0, 0]");

在JSON中.

in JSON.

更多推荐

为什么我会收到带有此代码的JsonReaderException?

本文发布于:2023-06-07 22:08:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/568655.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:我会   代码   JsonReaderException

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!