简体   繁体   中英

How can I read part of appsettings.json as raw string in .net core 5? [closed]

I use C# and .net core web api. I have following appsettings.json:

应用设置.json

How can I read the red part (as is, without parsing - raw text ) of this appsettings.json?

I know that I can put this in separate file and then read like this:

var googleServiceAccount = File.ReadAllText("GoogleServiceAccount.json");

But I want to put them in appsettings.json. Any ideas?

You can use Regex.Replace(string input, string pattern, string replacement); method to achieve your requirement:

var sb = new StringBuilder(); sb.AppendLine("{"); 
var googleServiceAccountItems = _configuration.GetSection("Google:ServiceAccount").Get<Dictionary<string, string>>(); foreach (var item in googleServiceAccountItems) { sb.AppendLine($"\t\"{item.Key}\": \"{item.Value}\""); }
sb.Append("}");

var GoogleServiceAccountString = sb.ToString();
GoogleServiceAccountString = Regex.Replace(GoogleServiceAccountString, "\n|\r|\t", String.Empty);

Your best bet is to change the type of the "ServiceAccount" field to a string, and then take what appears to be uninterpreted JSON and stringify it, escaping appropriately (shown is the \\" to escape the embedded quotes).

{
    "Google": {
        "ServiceAccount": "{ \"type\": \"x\", \"project_id\": \"x\" ... }"
    }
}

Also, you can't have multiline strings in JSON ,

    "ServiceAccount": "{
       "you": "cant",
       "do": "this"
    }"

so you'd have to insert \\\\n , which escapes the \\ itself leaving a newline \\n in the string or try one of the other suggestions in the referenced link.

{
    "Google": {
        "ServiceAccount": "{\\n \"type\": \"x\",\\n \"project_id\": \"x\" ... \\n}"
    }
}

If you really want to somehow designate a portion of the JSON as a string using some sort of magic (eg knowing that specific real pieces of JSON should be strings) then you'd have to build your own magic parser (custom configuration provider) that will essentially violate the JSON spec.

I highly recommend you don't do this as it will confuse everyone!

Try this

using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.Json;


IConfiguration configuration = new ConfigurationBuilder()
        .AddJsonFile(@"C:\....\appsettings.json")
        .Build();

 List<KeyValuePair<string,string>> items = configuration
        .GetSection("Google:ServiceAccount")
        .Get<Dictionary<string, string>>()
        .ToList();

foreach (var item in items) Console.WriteLine($"({item.Key},{item.Value})");

or you can use dependency injection instead of configuration builder

public class MyClass
{
   public MyClass(IConfiguration configuration)
{
List<KeyValuePair<string,string>> items = configuration
        .GetSection("Google:ServiceAccount")
        .Get<Dictionary<string, string>>()
        .ToList();
.....
}
}

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