myqq1418
忆水思寒
采纳率100%
2018-11-06 07:19

C# 怎么构造匿名对象转换成下面的Jason对象?

已采纳

想通过如下的匿名对象创造Jason对象,但像"sub_button"下面的最后两个大括号不知道怎么写
var date=new
{
var button = new
{
type = "click",
name = "今日歌曲",
key = "V1001_TODAY_MUSIC"
};
}

                    要构造的Jason对象如下:
                    {
 "button":[
 {    
      "type":"click",
      "name":"今日歌曲",
      "key":"V1001_TODAY_MUSIC"
  },
  {
       "name":"菜单",
       "sub_button":[
       {    
           "type":"view",
           "name":"搜索",
           "url":"http://www.soso.com/"
        },
        {
             "type":"miniprogram",
             "name":"wxa",
             "url":"http://mp.weixin.qq.com",
             "appid":"wx286b93c14bbf93aa",
             "pagepath":"pages/lunar/index"
         },
        {
           "type":"click",
           "name":"赞一下我们",
           "key":"V1001_GOOD"
        }]
   }]

}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • demmomo MoeMorlin 3年前
     var date = new
    {
        button = new List<object>
        {   
            new
            {
                type = "click",
                name = "今日歌曲",
                key = "V1001_TODAY_MUSIC"
            },
            new
            {                        
                name = "菜单",
                sub_button = new List<object>
                {
                    new {
                        type ="view",
                        name="搜索",
                        url="http://www.soso.com/"
                    },
                    new {
                        type="miniprogram",
                        name="wxa",
                        url="http://mp.weixin.qq.com",
                        appid="wx286b93c14bbf93aa",
                        pagepath="pages/lunar/index"
                    },
                    new {
                        type="click",
                        name="赞一下我们",
                        key="V1001_GOOD"
                    }
                }
            }
        }
    };
    
    
    点赞 3 评论 复制链接分享
  • caozhy 回答这么多问题就耍赖把我的积分一笔勾销了 3年前
    var button = new Object[]
    {
        new {
            type = "click",
            name = "今日歌曲",
            key = "V1001_TODAY_MUSIC"
        },
        new {
            name = "菜单",
            sub_button = new Object[]
            {
                new {    
                    type = "view",
                    name = "搜索",
                    url = "http://www.soso.com/"
                },
                new {
                    type = "miniprogram",
                    name = "wxa",
                    url = "http://mp.weixin.qq.com",
                    appid = "wx286b93c14bbf93aa",
                    pagepath = "pages/lunar/index"
                },
                new {
                    type = "click",
                    name = "赞一下我们",
                    key = "V1001_GOOD"
                }
            }
        }
    };
    
    点赞 1 评论 复制链接分享