1

How can I convert a list of strings into an Object?

var flattenList = string.Join(",", jsonVars);
var convJsonVars = JsonConvert.SerializeObject(flattenList);
convJsonVars = "{" + convJsonVars + "}";

var emailData = JsonConvert.DeserializeObject<object>(convJsonVars);

After I Serialize flattenList, these are the results:

"\"tenant_background_color\":\"#c41211\",\"tenant_logo_link\":\"https://imagelink.com\",\"tenant_font_color\":\"#FFFFFF\",\"tenant_name\":\"TNAME\",\"recipient_name\":\"User One\",\"login_link\":\"https://localhost:44330/\",\"verification_link\":\"https://localhost:44330/Link?Key=7b6c12fe-7658-45c5-a5c9-df9900231c6b-f7ea4ae9-3037-4dc5-98dd-e34a33770bb1\""

However, when I try to Deserialize it into an object type and submit it to Sendgrids api, I always get the error that I am passing them a string instead of an Object type.

EDIT:

This is what jsonVars looks like now:

"\"tenant_background_color\":\"#c41211\""

Its a list of strings

JianYA
  • 2,750
  • 8
  • 60
  • 136
  • Why, if you want to seralise an object, are your first flattening out into a string? It would be better to just Seralise jsonVars. – jason.kaisersmith Sep 26 '21 at 09:49
  • Because if I serialize the List of strings, I get a Json List of Objects instead of a Json Object only. – JianYA Sep 26 '21 at 09:54
  • Ok, but this approach also doesn't work. It would be useful if you show us how jsonVars looks today, and maybe give an example of how you want your output to look. Then we can see better and offer better help. – jason.kaisersmith Sep 26 '21 at 10:01
  • I edited my question to show what 1 entry of the list looks like. I just want an object with curly braces. – JianYA Sep 26 '21 at 10:06
  • *submit it to Sendgrids api*, what version and which API method are you using? – Józef Podlecki Sep 26 '21 at 10:16
  • see that [link](https://stackoverflow.com/questions/4535840/deserialize-json-object-into-dynamic-object-using-json-net) – Den Sep 26 '21 at 10:42
  • Does this answer your question? [Deserialize JSON object into dynamic object using Json.net](https://stackoverflow.com/questions/4535840/deserialize-json-object-into-dynamic-object-using-json-net) – Den Sep 26 '21 at 10:43
  • can you show the exact error you are getting for the sendGrids api ? – kumarmo2 Sep 26 '21 at 10:53

2 Answers2

1

when you desterilize a json string , the deserializer find the keys in json and populates fields with same name as json object key. the base model object doesn't have any field of it's own, maybe you are looking for a Dictionary<string,object> or a list of expandoObject:

public static void Main()
{
    var productos = "[{\"codigo\":\"Servilleta\",\"cantidad\":2},{\"codigo\":\"Papelhig\",\"cantidad\":1}]";
    var listProductos = JsonConvert.DeserializeObject<List<ExpandoObject>>(productos);
    foreach (dynamic prod in listProductos)
    {
        Console.WriteLine("Código: " + prod.codigo + " - Cantidad: " + prod.cantidad);
    }
}
Enigmativity
  • 113,464
  • 11
  • 89
  • 172
fmansour
  • 555
  • 3
  • 17
1

You are complicating things unnecessarily by calling JsonConvert.SerializeObject, given that you do not have an object but only properties that need to be joined and enclosed in curly brackets. So:

var convJsonVars = "{" + string.Join(",", jsonVars) + "}";

Now you can deserialize it in two ways that are equivalent in your case:

var emailData = JsonConvert.DeserializeObject<object>(convJsonVars);

or

var emailData = JObject.Parse(convJsonVars);

In both cases the returned object is of type JObject because you did not provide a schema during the Json deserialization. You can still access emailData properties using the indexer, i.e. emailData["tenant_background_color"].

Marco Luzzara
  • 5,540
  • 3
  • 16
  • 42