从json字符串写c#类 [英] Write c# class from a json string

查看:69
本文介绍了从json字符串写c#类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





能帮我写一下这个JSON内容的c#类:



Can you please help me to write the c# class for this JSON content :

{"pictureId":"656f33d5-8aec-4f50-ac01-e81e7435cf57","name":"A1","canEdit":true,"isOwner":true,"subComponents":[[2,"default:42a18f47-7033-432a-82ce-a5bd00f8f5f8"],[2,"default:f8c44f44-e716-4b17-b1b7-a5bd00b2007f"],[2,"default:94003fd5-b3d6-4c95-a540-a5bd00b18bdc"]],"aclPermissions":[],"version":0}





我希望能够将此对象写入将其序列化为JSON。



我尝试过:



我使用json2csharp生成类,但错误是使用子组件:



I want to be able to write this object to serialize it as a JSON.

What I have tried:

I used json2csharp to generate the class but the error is with subcomponent :

public class PictureRead
{
    public string pictureId { get; set; }
    public string name { get; set; }
    public bool canEdit { get; set; }
    public bool isOwner { get; set; }
    public List<list<object>> subComponents { get; set; }
    public List<object> aclPermissions { get; set; }
    public long version { get; set; }
}

推荐答案

Hi,

You can use multi-dimensional array to achieve this.

Below is the code to solve your problem.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication3
{
    public partial class WebForm4 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            PictureRead pictureRead = new PictureRead();
            pictureRead.pictureId = "656f33d5-8aec-4f50-ac01-e81e7435cf57";
            pictureRead.name = "A1";
            pictureRead.canEdit = true;
            pictureRead.isOwner = true;

            object[][] objsubComponents = new object[3][];
            objsubComponents[0] = new object[2] { 2, "default:42a18f47-7033-432a-82ce-a5bd00f8f5f8" };
            objsubComponents[1] = new object[2] { 2, "default:f8c44f44-e716-4b17-b1b7-a5bd00b2007f" };
            objsubComponents[2] = new object[2] { 2, "default:94003fd5-b3d6-4c95-a540-a5bd00b18bdc" };

            pictureRead.subComponents = objsubComponents;

            string json = Newtonsoft.Json.JsonConvert.SerializeObject(pictureRead);
        }
    }

    public class PictureRead
    {
        public PictureRead()
        {
            subComponents = new object[3][];
            aclPermissions = new object[0];
        }

        public string pictureId { get; set; }
        public string name { get; set; }
        public bool canEdit { get; set; }
        public bool isOwner { get; set; }
        public object[][] subComponents { get; set; }
        public object[] aclPermissions { get; set; }
        public int version { get; set; }
    }


}





谢谢,



Thanks,


这篇关于从json字符串写c#类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆