1

I have a string that contains JSON data and I want to convert it to string or string array in C#.

I'm getting exception of type JSONReaderException

Additional text encountered after finished reading JSON content: :. Path '', line 1, position 7

What is the meaning of it?

Here is my code:

string requestType = Request.QueryString[0].ToString();
JObject json = JObject.Parse(requestType);
JavaScriptSerializer j = new JavaScriptSerializer();
string b = JsonConvert.DeserializeObject<string>(requestType.Substring(1,requestType.Length-2));

The data is sent to the server from AJAX request. I'm attaching the request:

$.ajax({
                        url: "AJAXRequests.aspx",
                        type: "get",
                        data: JSON.stringify({ "first": "getevent","second":"data" }),
                        dataType:'json',
                        success: function (response){
                        },
                        error: function (xhr) {
                            alert("Problem in sending data to the server.\n Please check your internet connection and try again");
                        }
                    });
9
  • can you post your json data ? Commented Jul 1, 2015 at 7:31
  • @liorko post Json Please Commented Jul 1, 2015 at 7:35
  • Json is a string with a specific format. What is your input string and what do you expect as output? The code you posted doesn't help Commented Jul 1, 2015 at 7:43
  • As stated in the exception, the string probably contains some non-JSON data, so you may need to sanitize the string before trying to parse it. As others already suggested, please provide a sample of the string before expecting a more specific answer. Commented Jul 1, 2015 at 7:44
  • 1
    BTW I'm pretty certain you can't pass a json string as a URL query parameter without URL encoding it. Which means you'll have to decode it before trying to parse it Commented Jul 1, 2015 at 7:45

1 Answer 1

1

A JSONReaderException with the message "Additional text encountered after finished reading JSON content: :. Path '', line 1, position 7"

means,

the string you are parsing has some JSON at the start followed by something else that is not JSON.


In this case, the part that is not JSON starts a position 7 on line 1.

Sign up to request clarification or add additional context in comments.

2 Comments

Also note the OP is using a query string parameter as input. Either there is no valid json to begin with, or the OP has to decode it before parsing
@PanagiotisKanavos, agreed, the OP should probably be doing a POST and reading JSON from the body, if the Content-Type is appropriate. However, I'm answering the question that was asked.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.