【问题标题】:C# Error reading JObject from JsonReader. Path '', line 0, position 0C# 从 JsonReader 读取 JObject 时出错。路径 '',第 0 行,第 0 位置
【发布时间】:2019-11-13 00:10:14
【问题描述】:

我正在尝试使用下面的代码解析我的 json。我得到错误:

从 JsonReader 读取 JObject 时出错。路径 '',第 0 行,第 0 位置。

我认为这可能是因为我的 JSON 格式错误,所以我输出了它,它似乎还可以:

{ 
    "serviceDeskId": "4", 
    "requestTypeId": "223", 
    "requestFieldValues": { 
        "summary": "test" 
    } 
} 

但现在我完全被困住了。谁能看到我哪里出错了?这真让我抓狂!!

正是在这一行触发了错误:

var jsonresponse = JObject.Parse(response);

完整代码sn-p:

req.ContentType = "application/json";

                var json = JObject.Parse(
                        "{\"serviceDeskId\": \"4\",\"requestTypeId\": \"223\",\"requestFieldValues\": {\"summary\": \"" +
                        summary.Value + "\"}}");

                jsonCheck = json.ToString();

                using (var streamWriter = new StreamWriter(req.GetRequestStream()))
                    {

                        streamWriter.Write(json);
                    }

                    HttpWebResponse resp = req.GetResponse() as HttpWebResponse;


                    // Obtain a 'Stream' object associated with the response object.
                    Stream ReceiveStream = resp.GetResponseStream();

                    Encoding encode = System.Text.Encoding.GetEncoding("utf-8");

                    String response = "";

                    // Pipe the stream to a higher level stream reader with the required encoding format. 
                    StreamReader readStream = new StreamReader(ReceiveStream, encode);

                    response = readStream.ReadToEnd();

                    // Release the resources of stream object.
                    readStream.Close();

                    // Release the resources of response object.
                    resp.Close();

                    var jsonresponse = JObject.Parse(response);

任何帮助将不胜感激!

【问题讨论】:

  • 在尝试解析之前尝试输出对文本文件的响应(或者如果在 Visual Studio 中 - 添加断点并检查值) - jsonreader 无法将响应识别为有效的 json。如果有错误 - 你会看到它。
  • 服务器对您的 JSON 数据做了什么?它会简单地回声吗? IMO问题与编码(BOM)有关-也许服务器发送unicode而您正在阅读utf8?查看字节级别的响应。

标签: c# json stream streamreader jsonreader


【解决方案1】:

我只在输入完全为空字符串时看到此错误。检查您的文本是否适合该变量。

using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
                    
public class Program
{
    public static void Main()
    {
        var json = JObject.Parse("");
        Console.WriteLine(json);
        
    }
}

https://dotnetfiddle.net/u0FMLS

【讨论】:

    【解决方案2】:

    啊,我相信我以前遇到过这个问题。我发现 Visual Studio 以不同的方式保存 json 文件。您可以通过以下方式检查:

    1. 在 Visual Studio 中,转到文件 -> 打开并指向到您的 json 文件
    2. 然后单击“打开”按钮附近的小箭头并选择“打开方式..”
    3. 当“打开方式”对话框打开时,选择“二进制编辑器”并单击“确定”

    [注意:之前的步骤可以使用其他十六进制编辑器完成。]

    文件打开后,以 HEX 格式,查看它是以...{.. 开头还是以..}.. 结尾并删除起始点“..”和结束点“..”并保存文件并再试一次。

    如果您在 Visual Studio 中创建一个 json 文件,就会发生这种情况。

    或者,您可以使用其他程序(如 Notepad ++)创建一个新文件并使用该文件。

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-25
      • 2015-05-12
      • 1970-01-01
      • 1970-01-01
      • 2017-02-13
      • 2016-04-13
      相关资源
      最近更新 更多