简体   繁体   中英

System.Text.Json.JsonException: The input does not contain any JSON tokens

I'm just trying to use a Http POST method in a Blazor app through

public async Task CreateUnit(UnitEntity unit)
{
    await _http.PostJsonAsync<UnitEntity>("api/units", unit);
}

_http and myObject have been defined elsewhere, but I'm getting this weird error. Can anyone help? This is the closest thing I could find elsewhere: https://github.com/do.net/runtime/issues/30945 .

The full error message is

System.Text.Json.JsonException: The input does not contain any JSON tokens. Expected the input to start with a valid JSON token, when isFinalBlock is true. Path: $ | LineNumber: 0 | BytePositionInLine: 0.

And it here's the stack

在此处输入图像描述

这个错误可能弹出的另一个原因,就像它对我所做的那样,仅仅是因为 API 端点不存在,因为它拼写错误。

I got a similar error in Program.cs Main method CreateHostBuilder(args).Build();:

System.FormatException: 'Could not parse the JSON file.'

JsonReaderException: The input does not contain any JSON tokens. Expected the input to start with a valid JSON token, when isFinalBlock is true. LineNumber: 0 | BytePositionInLine: 0.

For me it turned out to be the local secrets.json file that not contained a valid json object.

https://docs.microsoft.com/en-us/aspnet/core/security/app-secrets?view=aspnetcore-5.0&tabs=windows#secret-manager

Because of this I could not see any errors in Git or rollback to a working commit since the file is not checked in.

Solved by adding an empty object to the file via Visual Studio - right click the project in solution explorer and select Manage User Secrets :

{

}

https://docs.microsoft.com/en-us/aspnet/core/security/app-secrets?view=aspnetcore-5.0&tabs=windows#manage-user-secrets-with-visual-studio

The comment from agua from mars was right. This stopped the error and then I'd managed to forget to commit my database transaction as well so there was no other exception, but it wasn't going into the database. All fixed now though :)

i had similar issue and the problem to check if the json string you are readying is empty, null, or bad formatted. debug to the code line where you are reading data into string before deserialize or serialize call.

I got this error when communicating between two APIs.

request = await req.DeserializeRequestBodyAsync<MyDto>(jsonSerializerOptions);

Turned out the code below did not actually send any values:

httpRequestMessage.Content = JsonContent.Create(myDto);
var httpClient = _clientFactory.CreateClient();
var httpResponseMessage = await httpClient.SendAsync(httpRequestMessage, cancellationToken);

I had to manually specify:

await httpRequestMessage.Content.LoadIntoBufferAsync();

Like this:

httpRequestMessage.Content = JsonContent.Create(myDto);
await httpRequestMessage.Content.LoadIntoBufferAsync();
var httpClient = _clientFactory.CreateClient();
var httpResponseMessage = await httpClient.SendAsync(httpRequestMessage, cancellationToken);

For me, this error occurred when calling FindByNameAsync of UserManager.

Sounds silly, but the database connection string in the appsettings was wrong!

Late answer - but I ran into this using Blazor WebAssembly with Browser Link (trying to get Hot Reload to work). Turns out it's an issue loading the appsettings and Browser Link was expecting the secrets file. I fixed by right clicking the Server project and copy/pasting my appsettings values into the secrets file.

In my case, I was passing the id of my object along with the object itself in the url of the put request to an API and faced the same exception. It turned out that it was not necessary to pass the id (as it was retrieved from the object itself, in fact it was not present in the method signature). Removing the id solved the problem.

In my case the code was doing this:

var json = await response.Content.ReadAsStringAsync();
var result = JsonObject.Parse(json); // threw the exception mentioned in the question

Why did that happen? That's because json value was an empty string "" . Parse fails with an empty string.

Fixed it doing this simple change:

var json = await response.Content.ReadAsStringAsync();
var result = string.IsNullOrEmpty(json) ? null : JsonObject.Parse(json);

This error occurred when communicating between client and web API.

API code:

public async Task<IActionResult> PostAsync(object review)
{
    return Ok();
}

Client code:

var res = await _client.PostAsJsonAsync("api/reviews", review);
if (res.IsSuccessStatusCode)
{
    var myObject = await res.Content.ReadFromJsonAsync<MyObject>(); //this line threw mentioned error
}

Turned out that the API endpoint was returning something different compared to what I was trying to read from JSON ie I was trying to read MyObject but API was returning ActionResult

In my case database column was marked not null and I was passing null in API.

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