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

8
  • Why, if you want to seralise an object, are your first flattening out into a string? It would be better to just Seralise jsonVars. Commented Sep 26, 2021 at 9:49
  • Because if I serialize the List of strings, I get a Json List of Objects instead of a Json Object only. Commented Sep 26, 2021 at 9: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. Commented Sep 26, 2021 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. Commented Sep 26, 2021 at 10:06
  • submit it to Sendgrids api, what version and which API method are you using? Commented Sep 26, 2021 at 10:16

2 Answers 2

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"].

Sign up to request clarification or add additional context in comments.

1 Comment

Hi Marco, you were right. It worked and all I needed to do was put the curly brackets.
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);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.