无法获取Dictionary键,C#中的字符串值

时间:2015-07-14 05:18:47

标签: c# asp.net json

我有代码在C#对象中获取json对象。在req object中,我有一个名为数字的对象。每个数字都有1个键和3个值:

  1. 日期
  2. 递减
  3. 状态
  4. 如何获取每个数字的密钥及其3个值? 我想要数字,日期,desc,状态字段

    http://postimg.org/image/j4x36aotd/

    json

    {
        "requestId": "546b384ce51f469a2e8b4567",
        "numbers": {
            "917566559950": {
                "date": "2014-11-18 17:45:59",
                "status": 1,
                "desc": "DELIVERED"
            }
        }
    }
    

    C#代码

    public partial class All_GetSmsReeport : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string json = "";
            if (Request.QueryString["data"] !="")
            {
                json = Request.QueryString["data"];
    
                var req = JsonConvert.DeserializeObject<Request>(json);
                string requestid = req.requestId;
    
            }
        }
    
    }
    
    public class smsstatus
    {
        public string date { get; set; }
        public int status { get; set; }
        public string desc { get; set; }
    }
    
    
    public class Request
    {
        public string requestId { get; set; }
        public Dictionary<string, smsstatus> numbers { get; set; } //<-- See this line
    }
    

3 个答案:

答案 0 :(得分:2)

var req = JsonConvert.DeserializeObject<Request>(json);
foreach (var kv in req.numbers)
{
    Console.WriteLine("Key:{0} Date:{1} Status:{2} Desc:{3}", kv.Key, kv.Value.date, kv.Value.status, kv.Value.desc);
}

如何循环

答案 1 :(得分:1)

简单的forach循环迭代。

var i, item;
for (i = items.length-1; i >= 0; i--) {
  item = items[i];
  if (item.active===false || item.id = 9) {
    items.splice(i, 1);
  }
}

答案 2 :(得分:0)

            Try this:
                var t = JsonConvert.DeserializeObject<Request>(JsonConvert.SerializeObject(myObject));
                foreach (var item in t.numbers)
                {
                    Console.Write(item.Key);
                    Console.WriteLine(item.Value.date + ":" + item.Value.desc + ":" + item.Value.status);
                }