简体   繁体   中英

Json Converting complex dictionary<customobject, string>

U am facing a little Json conversion problem using Newtonsoft in Azure Functions.

I have an HttpTrigger Function and am receiving a json string and convert it into my complex object afterwards.

My goal is to be able to convert my json string (below) to a c# Dictionary<AppLanguage, string>() object.

I have the feeling it cannot read the integer in key and convert it to my enum value. So i guess there's a configuration for that?

 System.Private.CoreLib: Exception while executing function: MyAppBranchFunctions. Newtonsoft.Json: Unexpected token StartArray when parsing enum. Path 'Texts.Tutorials', line 1, position 382828.

The json looks something like this:

{
"xxx":
    {
    "Texts":{"Tutorials":[{"Key":1,"Value":"tutorial"},{"Key":4,"Value":""},{"Key":5,"Value":""},{"Key":6,"Value":""},{"Key":7,"Value":""},{"Key":8,"Value":""},{"Key":9,"Value":""},{"Key":10,"Value":""},{"Key":2,"Value":""},{"Key":3,"Value":""}]
    }
}

The signatures of the classes looks as follow:

public class Texts
    {
    [JsonConverter(typeof(StringEnumConverter))]
    public Dictionary<AppLanguage, string> Tutorials { get; set; }
    }

[JsonConverter(typeof(StringEnumConverter))]
    public enum AppLanguage
    {
        /// <summary>
        /// german
        /// </summary>
        [EnumMember(Value = "de")]
        de,
        /// <summary>
        /// english
        /// </summary>
        [EnumMember(Value = "en")]
        en,
        /// <summary>
        /// french
        /// </summary>
        [EnumMember(Value = "fr")]
        fr,

In my case I had to serialize my json object to actual json before I add it as JsonBody to my RestSharp Post. I had like request.AddJsonBody(myObject) instead of request.AddJsonBody(jsonOfMyObject) which solved my problem now.

Should've posted my "post" code as well i guess.

Thanks for the replies.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM