Click here to Skip to main content
15,888,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,

Can you please help me to write the c# class for this JSON content :
C#
{"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}


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 :
C#
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; }
}
Posted
Updated 2-Mar-16 11:54am
v2
Comments
Mathi Mani 2-Mar-16 15:24pm    
Can you please share what the error is?
Member 12330910 2-Mar-16 15:26pm    
i can't write subComponents item
Matt T Heffron 2-Mar-16 16:08pm    
It isn't clear what is still wrong.
Is the problem that when you have deserialized the JSON to an instance of your PictureRead class, that it doesn't serialize back to the same JSON?
It's probably time to use the debugger to see what's really going on.
BillWoodruff 2-Mar-16 15:42pm    
Try using object[] instead of List<object> ... that's a "wild guess."
Richard Deeming 2-Mar-16 16:09pm    
Your class works fine with JSON.NET[^] - it deserializes the sample JSON, and reserializes it exactly as expected.

If you can't switch to JSON.NET, then you'll need to tell us what JSON library you're using, and the full details of the error.

1 solution

C#
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,
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900