Announcements
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++ ?
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++?
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"}];
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).
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.
André Arnaud de Cal...
294,125
Super User 2025 Season 1
Martin Dráb
232,871
Most Valuable Professional
nmaenpaa
101,158
Moderator