Could not parse the JSON file ,Error in Progam.cs

2020-06-16 02:50发布

问题:

I have some problem with program.cs new version in ASP.CORE 2.0

Here my code

     public class Program
{
    public static void Main(string[] args)
    {
        BuildWebHost(args).Run();
    }

    public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseIISIntegration()
            .UseKestrel()
            .UseStartup<Startup>()
            .UseConfiguration(new ConfigurationBuilder().AddCommandLine(args).Build())
            .Build();

}

When i start project i has an error

System.FormatException: 'Could not parse the JSON file. Error on line number '0': ''.'

How to resolve it?

回答1:

I had a a similar problem as this in another thread and posted my solution over there: Application Startup Failure with Json read Error. Posting it here as well in case it disappears.


I came across the same problem. In my case, I'd started implementing app secrets but left it halfway through. My secrets.json file was left linked but with invalid JSON.

Check your .csproj to see if a <UserSecretId> property is set under <PropertyGroup>. If it's set, BuildWebHost() will look through your secrets.json file in '%APPDATA%\Microsoft\UserSecrets\{secretId}', in addition to your appsettings.json file. An error in either file will cause the method to fail, but it won't tell you which file it is.

The solutions in my case were either to remove the <UserSecretId> property or



回答2:

Is your appSettings.json file (or whatever config you are using) formatted properly? WebHost.CreateDefaultBuilder will not be able to parse it correctly if there are invalid characters at the start. I saw this issue when a copy/paste added a space at the beginning of the file.



回答3:

There is a possibility it might happen when appsettings.json is not properly formated

In my case, I had the below configuration and got the error

 {
"ConnectionStrings": {
"TransferDBConnection": 
 "Server=INGBTCPIC5DT04D;Database=TransferDB;Trusted_Connection=true;
 },
  ***{***
   "Logging": {
   "LogLevel": {
    "Default": "Warning"
   }
 },
 "AllowedHosts": "*"
}

ideally, it should be one extra { cause this problem

 {
 "ConnectionStrings": {
 "TransferDBConnection": 
 "Server=INGBTCPIC5DT04D;Database=TransferDB;Trusted_Connection=true;"
 },
  "Logging": {
  "LogLevel": {
    "Default": "Warning"
  }
},
"AllowedHosts": "*"
}


回答4:

I had this problem and the reason is that for some reason Visual Studio saves files with some invisible metadata that goes before the text therefore JSON reader cannot parse it.

You need to File -> Save As -> (Choose UTF-8 Without BOM).



回答5:

Same problem and solved by removing <UserSecretsId> </UserSecretsId> from *.cproj.

It was a bad user secret configuration



回答6:

I had the same problem.
I found that this problem seemed to have been caused by my appsettings.json file.
I copied a version from another place to solve this problem.



回答7:

Check if you don't have any hidden spaces or tabs in your json. You can use https://jsonlint.com/ to verify your json format.



回答8:

in m case I put extra { right before (above) "ConnectionStrings" : in appsettings.json

I copied this from another file and assumed that "ConnectionStrings" : must inside a curly braces {}

before

...
},
{  //<-- this one the culprit
    "ConnectionStrings": {
      "Dapper": "Server=.;Database=Dapper;Trusted_Connection=True;"
    },

after

...
},
    "ConnectionStrings": {
      "Dapper": "Server=.;Database=Dapper;Trusted_Connection=True;"
    },


标签: c# asp.net json