简体   繁体   中英

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

I'm trying to parse my json with the code below. I get the error:

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

I thought this might be because my JSON was malformed, so I have output it, and it appears to be ok:

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

But now I'm completely stuck. Can anyone see where I'm going wrong? This is driving me crazy!!

It's on this line that the error is triggered:

var jsonresponse = JObject.Parse(response);

Complete code snippet:

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);

Any help would be appreciated!

I've only seen this error when the input is exactly empty string. Check that your text is making it to that varible.

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

Ah I believe I had this issue before. What I found out is that Visual Studio saves the json files in a different way. You can check this in the following way:

  1. In Visual Studio go to File -> Open and point to your json file
  2. Then click the small arrow near the "Open" button and choose "Open With.."
  3. When the "Open With" dialog opens choose "Binary Editor" and click OK

[NOTE: The previous steps could have been accomplished with using some other Hex editor.]

After the file is opened, in its HEX format, see if it starts with ...{.. or ends with ..}.. and remove the starting dots ".." and the ending dots ".." and save the file and try again.

This happens if you create a json file inside Visual Studio.

Alternatively you can create a new file with some other program (like Notepad ++) and use that file.

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM