zoukankan      html  css  js  c++  java
  • Newtonsoft.Json 动态解析 json字符串

    有一个json字符串是动态的,如下面,columns中的数量是不固定的,因此就不能使用反序列化类的方法了:

    因此使用这样一种方式,把columns中的所有东西都输出出来:

    public void GetDataFormHttp()
            {
    
                string result= "json字符串";//结构是上图中的
                JObject o = JObject.Parse(result);
                string status = o.SelectToken("status").Value<string>();
                if (status=="ok")
                {
                    //构造泛型用于存放数据
                    //var ht = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
                    IDictionary<string, JToken> rates = (JObject)o["columns"];
                    Dictionary<string, string> dictionary = rates.ToDictionary(pair => pair.Key, pair => (string)pair.Value);
                    StringBuilder sb = new StringBuilder();
                    foreach (KeyValuePair<string, string> kv in dictionary)
                    {
                        Console.WriteLine(kv.Key + kv.Value);
                        sb.Append("<tr>");
                        sb.AppendFormat("<td>{0}", kv.Key);
                        sb.Append("</td>");
                        sb.AppendFormat("<td>{0}", kv.Value);
                        sb.Append("</td>");
                        sb.Append("</tr>");
                    }
                    ltl.Text = sb.ToString();
                }
  • 相关阅读:
    73. Set Matrix Zeroes
    289. Game of Live
    212. Word Search II
    79. Word Search
    142. Linked List Cycle II
    141. Linked List Cycle
    287. Find the Duplicate Number
    260. Single Number III
    137. Single Number II
    Oracle EBS中有关Form的触发器的执行顺序
  • 原文地址:https://www.cnblogs.com/25miao/p/7647408.html
Copyright © 2011-2022 走看看