无法获取c#中字符串中的字典键值

本文关键字:字典 键值 字符串 获取 | 更新日期: 2023-09-27 18:02:28

我有代码在c#对象中获得json对象。在req object中,我有一个叫做number的对象。每个数字有1个键和3个值:

  1. desc
  2. <
  3. 状态/gh>

我如何得到每个数字的关键和它的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
}

无法获取c#中字符串中的字典键值

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);
}

如何循环

简单的forach循环迭代。

foreach (var numberObj in req.numbers)
        {
            var key = numberObj.Key;
            var value = numberObj.Value;
            var status = value.status;
            var date = value.date;
            var descr = value.desc;
        }
            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);
                }