1

I have the following string. I am getting the following error. Could you please let me know what could be wrong?

Unexpected character encountered while parsing value: C. Path '', line 0, position 0.
JsonTextReader error: System.Exception {Newtonsoft.Json.JsonReaderException}

Here is the JSON string I am getting from client.

Content-Type: application/json
Content-Disposition: attachment; filename="postData.json"

{"name":"test44","age":"66","gender":"B","dob":"10\/10\/2003","file":null}

Here is my code for parsing using JSON.NET.

JsonTextReader reader = new JsonTextReader(new StringReader(json));
while (reader.Read())
{
     if (reader.Value != null)
        Console.WriteLine("Token: {0}, Value: {1}", reader.TokenType, reader.Value);
     else
        Console.WriteLine("Token: {0}", reader.TokenType);
}
0

2 Answers 2

5

You've included the HTTP headers in your JSON string - you don't want those. Your json value should just be this:

{"name":"test44","age":"66","gender":"B","dob":"10\/10\/2003","file":null}

I've tested your code, and when including the headers I get the same exception as you, but without them it's fine.

You should look at how you're receiving the data to start with - it would normally be odd to get just those two headers along with the body. You haven't told us how the client is providing the data, but if they're giving those headers where they should just be giving the body, then it's a client error.

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

6 Comments

if some important auth are putting in headers how to ignore this one.it's not solution to remove headers. if u have any other solution than let me know .
@mehtamehta: You handle those separately - you don't try to parse them as JSON. You'd typically get them separately in the response anyway.
exactly i have problem likes when i try to disconnect device still some json parsing are doing in background after that my app got crash. Unhandled Exception: Newtonsoft.Json.JsonReaderException: Unterminated string. Expected delimiter: ". Path 'result.resourceJobs[45].job.creationTime', line 1, position 40271. occurred if u have any idea how to handle please let me know
@mehtamehta: You should be asking a new question with details. It's pretty much unrelated to this question as far as I can tell. (But yes, if you try to parse a partial string, that will fail...)
can you please check already asked this question in below link: stackoverflow.com/questions/49292589/… but i did not get any solution for this if u have please share ur views with me
|
1

Looks to me like it's reading the 'C' from the Content type declaration & is complaining, don't include http headers.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.