Click here to Skip to main content
15,881,898 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have a model class by which i bind my List<classname> and it look like this

public class Response
{
    public vMetaData vData { get; set; }
    public List<Data> data { get; set; }
}


public class vMetaData
{
    public string version { get; set; }
}

public class Data
{
    public string ID { get; set; }
    public List<studentData> studentData { get; set; }
    public Dictionary<string,dynamic> FeeData { get; set; }
}


public class studentData
{
    public string StudentID { get; set; }
    public string StudentName { get; set; }
}


public class FeeData
{
    public string key { get; set; }
    public object value { get; set; }
}



now i have one list for studentdata and one dictionary for feedata

i need below json as a final result:-

{
	"Vdata": {
		"version": "1.0",
	},
	"data": [
		{
			"ID": "0001",
			"studentData": [
				{
					"StudentID": "0001",
					"StudentName": "std1"
				}
			],
			"FeeData ": [
				{
					"key": "feeStatus",
					"value": "Submit"
				},
				{
					"key": "StudentStatus",
					"value": "Active"
				},
			]
		},
		
		{
			"ID": "0002",
			"studentData": [
				{
					"StudentID": "0002",
					"StudentName": "std2"
				}
			],
			"FeeData ": [
				{
					"key": "feeStatus",
					"value": "Submit"
				},
				{
					"key": "StudentStatus",
					"value": "InActive"
				},
			]
		}
	]
}


how can i convert my data into this format, can any one suggest me

What I have tried:

i tried this but it create new object on every loop so only last value is bind.

for (int i = 0; i < studentdata.Count; i++)
   {

       var studentitem = "["+ JsonConvert.SerializeObject(studentdata[i])+ "]";
       List<studentData> student_data = JsonConvert.DeserializeObject<List<studentData>>(studentitem);


      var item = JsonConvert.SerializeObject(FeeData[i]);
      Feeitem = item.Replace("[", "").Replace("]","");
      var feedata = JsonConvert.DeserializeObject<Dictionary<string, dynamic>>(Feeitem);




       Response = new Response
      {
          vData = new vMetaData
          {
              version = "1.0",
          },

          data = new List<data> {
               new data { entRefIDs = EntitilementRefId, studentData = student_data,FeeData=feedata }
          }
      };
   }
   return Response;
Posted
Updated 20-Sep-22 0:50am
v2
Comments
Tony Hill 20-Sep-22 7:57am    
If you have an already populated object graph then just serialize the root object, I fail understand why you are messing about with all the Serialize/Deserialize calls and the string replace calls.
George Swan 21-Sep-22 1:15am    
Are you just trying to return a single populated instance of the Response class? If you are, there is no need to employ Json

1 solution

List<object> Jsonobject = new List<object>();


string Json = JsonConvert.SerializeObject(YourClassUwantToSerialize);
    Jsonobject .Add(Json);



Now you can print
WriteLine("[");
               foreach (var item in Jsonobject )
               {
                   count--;
                   sw.WriteLine(item);
                   if (count != 0)
                       sw.WriteLine(",");
               }
               WriteLine("]");
 
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