将IEnumerable <dynamic>转换为数组C#

时间:2016-02-13 06:46:31

标签: c#

我有一个IEnumerable<dynamic>,我从一个使用json.net序列化的dapper查询得到如下所示。

[{"id": "ZoomIn", "label": "Zoom In"},
        {"id": "ZoomOut", "label": "Zoom Out"},
        {"id": "OriginalView", "label": "Original View"}]

我希望json采用以下格式..

[["ZoomIn","Zoom In"],
["ZoomOut","Zoom Out"],
["OriginalView","Original View"]]

真心感谢任何帮助。

由于

1 个答案:

答案 0 :(得分:0)

使用JavaScriptSerializer的简单代码段,

public class Friends
    {

        public List<FacebookFriend> data {get;set;}
    }

    public class FacebookFriend
    {

        public string id {get;set;}
        public string label{get;set;}
    }

    Friends facebookFriends = new JavaScriptSerializer().Deserialize<Friends>(result);

    string json=
        @"{""data"":[{"id": "ZoomIn", "label": "Zoom In"},
            {"id": "ZoomOut", "label": "Zoom Out"},
            {"id": "OriginalView", "label": "Original View"}]}";


    Friends facebookFriends = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<Friends>(json);

    foreach(var item in facebookFriends.data)
    {
       Console.WriteLine("id: {0}, label: {1}",item.id,item.label);
    }