Serialize anonymous objects for upload resposne from server .NET

Serialize anonymous objects for upload resposne from server .NET

mkwatsmkwats Posts: 4Questions: 2Answers: 0

I am trying to create a server response from the upload files post from Editor. I am using https://editor.datatables.net/manual/server as the basis for the resposne.

C# does not let me have Variable as the Key -- the "1" object below. Is there a solution to this? Note it also cannot be a number, but this I can get around with an undersocre and bit of server side manipualtion.

C# server side example:
JsonConvert.SerializeObject(new { 1 = new { filename = "Screen Shot.png" ...} } )
The 1 cannot be a variable (the File Id as suggested in the example).

Any advise - hints to a solution?

{
"files": {
"files": {
** "1": **{
"filename": "Screen Shot.png",
"web_path": "\/upload\/1.png"
},
**"2": **{
"filename": "Screen .png",
"web_path": "\/uplo.png"
}
}
},
"upload": {
"id": "1",
"id": "2"
}
}

Answers

  • mkwatsmkwats Posts: 4Questions: 2Answers: 0
    edited October 2021

    Got there using NewtonSoft:

                int id = 12;  //id from the database
                string ID = id.ToString();  //change to string
    
                dynamic ret = new JObject(); //create dynamic object
                ret = new JObject(new JProperty("files", new JObject())); 
                ret.files.Add(new JProperty("Key", "value")); //example simple key
                ret.files.Add(new JProperty( ID, json_object));  // ID from variable
    
                return Content(JsonConvert.SerializeObject(ret), "application/json");
    
Sign In or Register to comment.