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

标签 c# json stream streamreader jsonreader

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

Error reading JObject from JsonReader. Path '', line 0, position 0.



我想这可能是因为我的JSON格式不正确,所以我输出了它,看起来没问题:
{ 
    "serviceDeskId": "4", 
    "requestTypeId": "223", 
    "requestFieldValues": { 
        "summary": "test" 
    } 
} 

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

正是在这一行触发了错误:
var jsonresponse = JObject.Parse(response);

完整的代码片段:
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);

任何帮助,将不胜感激!

最佳答案

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

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

关于C# 从 JsonReader 读取 JObject 时出错。路径 '' ,第 0 行,位置 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56850864/

相关文章:

c# - 将json字符串转换为字符串数组

ruby-on-rails - Rails 3.2 流媒体

java - 读取 MIME 编码流的正确方法是什么?

c# - 将强类型配置设置直接访问到 ASP.NET 5 (vNext) 中的类库中?

c# - 如何将参数传递给 C# 中的公共(public)类?

c# - 选择什么 .NET 版本?

c# - 从 LINQ DataContext 中的表名获取表数据

php - 未通过 PHP 将 Json 对象中的所有数据获取到数据库中

javascript - 尝试从 meteor 中的vimeo api返回json数据

c++ - 文件中的字数,C++