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

Newtonsoft.Json  动态解析 json字符串

因此使用这样一种方式,把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();
            }

 

相关文章:

  • 2022-12-23
  • 2021-12-06
  • 2021-05-30
  • 2021-10-06
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-01-30
  • 2022-12-23
  • 2022-12-23
  • 2022-01-01
  • 2022-02-25
相关资源
相似解决方案