添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
含蓄的莲藕  ·  [PyQt] ...·  2 周前    · 
霸气的铅笔  ·  CRUD with C#/SQL in ...·  2 周前    · 
玩篮球的圣诞树  ·  Vercel dev CLI using ...·  2 周前    · 
细心的丝瓜  ·  Windows crash in ...·  2 天前    · 
奔跑的警车  ·  Cannot start Weston & ...·  2 天前    · 
悲伤的拐杖  ·  使用 Remitly ...·  2 月前    · 
沉着的烈马  ·  impala mybatis ...·  3 月前    · 
string name = person["name"].ToString();
int age = person["age"].Value<int>();
Console.WriteLine($"Name: {name}, Age: {age}");
    Enter fullscreen mode
    Exit fullscreen mode
JObject nestedObject = JObject.Parse(@"{ 'person': { 'name': 'John', 'age': 30 } }");
JToken nameToken = nestedObject.SelectToken("$.person.name");
Console.WriteLine(nameToken.ToString()); // Output: John
    Enter fullscreen mode
    Exit fullscreen mode
  "ApiSettings": {
    "TwitterApiKey": "your-api-key",
    "TwitterApiSecret": "your-api-secret",
    "BearerToken": "your-bearer-token"
    Enter fullscreen mode
    Exit fullscreen mode
    static void Main(string[] args)
        var json = File.ReadAllText("appsettings.json");
        JObject config = JObject.Parse(json);
        var apiSettings = config["ApiSettings"];
        string apiKey = apiSettings["TwitterApiKey"].ToString();
        string apiSecret = apiSettings["TwitterApiSecret"].ToString();
        string bearerToken = apiSettings["BearerToken"].ToString();
        Console.WriteLine($"API Key: {apiKey}");
        Console.WriteLine($"API Secret: {apiSecret}");
        Console.WriteLine($"Bearer Token: {bearerToken}");
    Enter fullscreen mode
    Exit fullscreen mode
   if (config["ApiSettings"] == null)
       throw new Exception("ApiSettings section is missing in appsettings.json");
    Enter fullscreen mode
    Exit fullscreen mode
       public string TwitterApiKey { get; set; }
       public string TwitterApiSecret { get; set; }
       public string BearerToken { get; set; }
    Enter fullscreen mode
    Exit fullscreen mode

The JObject class in Newtonsoft.Json is a powerful tool for working with JSON in C#. From basic parsing and manipulation to advanced usage and best practices, this guide provides a solid foundation. Keep experimenting and applying these techniques in your projects to handle JSON data efficiently. Happy coding!

Built on Forem — the open source software that powers DEV and other inclusive communities.

Made with love and Ruby on Rails. DEV Community © 2016 - 2024.