zoukankan      html  css  js  c++  java
  • C# parser JSON get Key and value

    /***********************************************************************
     *               C# parser JSON get Key and value
     * 说明:
     *     将配置放置在JSON文件中,通过Json.NET来解析JSON数据,由于配置是随时
     * 可能被改变,而且key也不是固定的,所以需要动态获取key、value来判断要怎么
     * 进行处理,不过在解析的时候发现JToken不能直接获取到Key、Value,通过JObject
     * 来进行获取。
     * 
     *                              2017-8-7 深圳 龙华樟坑村 曾剑锋
     **********************************************************************/
    
    
    一、参考文档:
        一、NuGet Package Manager UI
            https://docs.microsoft.com/zh-cn/nuget/tools/package-manager-ui
        二、Introduction
            http://www.newtonsoft.com/json/help/html/Introduction.htm
        三、Read JSON from a file
            http://www.newtonsoft.com/json/help/html/ReadJson.htm
        四、Getting the name / key of a JToken with JSON.net
            https://stackoverflow.com/questions/21002297/getting-the-name-key-of-a-jtoken-with-json-net?answertab=votes
        五、C# Newtonsoft.Json之LINQ To Json实例(一)
            http://blog.csdn.net/u011127019/article/details/52486867
        六、使用JSON.net获取JToken的名称/键
            https://gxnotes.com/article/90134.html
    
    二、Demo Code:
        using Newtonsoft.Json.Linq;
        
        ...
    
        JObject systemConfig = JObject.Parse(File.ReadAllText("config/system_config.json"));
    
        foreach (JToken item in systemConfig["display"]["resolution"])
        {
            Console.WriteLine(item.ToString());
    
            // 重新合成JObject对象来提取Key、Value
            JObject obj = JObject.Parse("{" + item.ToString() + "}");
            foreach(var pair in obj)
            {
                Console.WriteLine(pair.Key + "," + pair.Value);
            }
        }
  • 相关阅读:
    LinqToXML~读XML文件
    C#~使用FileSystemWatcher来监视文件系统的变化
    HDU4144:Bacon's Cipher
    国产手机的路还很长
    同一个存储过程中,不能多次select into 到同一张表的问题
    IT行业为什么没有进度
    IOT(Index Organized Table)
    模版——容器,迭代器
    shell split分析日志文件
    VMware vSphere 服务器虚拟化之十七 桌面虚拟化之安装View链接服务器
  • 原文地址:https://www.cnblogs.com/zengjfgit/p/7299890.html
Copyright © 2011-2022 走看看