web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

assign value to C# list<KeyValuePair<string,string> from X++

(0) ShareShare
ReportReport
Posted on by 225

Hello,

I have C# class. in that I have two properties of type "List<KeyValuePair<string,string>>" as shown in below screenshot. can anyone tell me how I can assign value to this from X++ ?

pastedimage1614361187741v1.png

In X++ I have created object of this class. and I want to assign value like:

object.ViewPrinciplesKeyValue = [{"Key":"Everyone","Value":"Everyone"}].

Is there anything like JsonArray, JsonObject in X++?

I have the same question (0)
  • Martin Dráb Profile Picture
    236,297 Most Valuable Professional on at
    RE: assign value to C# list<KeyValuePair<string,string> from X++

    X++ doesn't officially support generics, therefore working with generic types from X++ isn't easy. Usually the best option is providing an alternative, non-generic interface that you can easily call from X++.

    By the way, does C# support an implicit deserialization of strings to JsonProperty objects, as your examples suggests? I would think that you need to explicitly call a deserializer (e.g. JsonConvert.DeserializeObject).

    Anyway, if you intention is to assign a string, create a string property (such as ViewPrinciplesJson) which will do the deserialization and assing the value of ViewPrinciplesKeyValue.

  • Pratik Rami Profile Picture
    225 on at
    RE: assign value to C# list<KeyValuePair<string,string> from X++

    We have to call JsonConvert.DeserializeObject in C#.

    Martin, sorry but i am not able to understand what you said in last para. Can you please explain little more. I have to assign list to this property.

    What I am doing is: I create object of this class in X++...assign all properties values and then send this object as parameter to C# method which will call external api. In that method this object will be serialised and used as a requestData(body).

  • Verified answer
    Martin Dráb Profile Picture
    236,297 Most Valuable Professional on at
    RE: assign value to C# list<KeyValuePair<string,string> from X++

    You said you wanted to assign a string. To do that, you need to call JsonConvert.DeserializeObject to convert the string to a generic list. So why don't you wrap this logic in a string property of the C# class? It could look somehow like this:

    public string ViewPrinciplesJson
    {
        get
        {
            return JsonConvert.SerializeObject(ViewPrinciplesKeyValue);
        }
        set
        {
            ViewPrinciplesKeyValue = JsonConvert.DeserializeObject(value);
        }
    }

    Using such a property from X is trivial - you'll simply assign a string to it, as you asked for:

    object.ViewPrinciplesJson = [{"Key":"Everyone","Value":"Everyone"}];

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
CA Neeraj Kumar Profile Picture

CA Neeraj Kumar 2,167

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 867 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 617 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans