我正在尝试使用以下代码解析我的 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/