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

2

6

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!

Barye answered 2/7, 2019 at 10:53 Comment(2)
Try to output response to text file (or if in visual studio - add breakpoint and check value) before trying to parse it - jsonreader does not recognise response as valid json. If there's an error - you'll see it.Stimulate
What's the server doing with your JSON data? Will it simply echo? IMO the problem is related to encoding (BOM) - maybe the server sends unicode and you're reading utf8? Have a look at the response on the byte level.Tempt
B
1

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

Buckman answered 12/1, 2021 at 17:48 Comment(0)
S
0

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.

Strangle answered 2/7, 2019 at 11:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.