Announcements
Hello
Can you please help me understand, how I can write code in F&O (X++) to convert below C# code (JSON) to X++ and deserialize it. Also how I can loop through all the multiple records in Json to assign/populate value from json record to AX table field.?
I know we can write a contract class, but Im confused whether to write multiple contract class (as we wrote in C# ?)
public class Project
{
public int id { get; set; }
public string name { get; set; }
}
public class Entry
{
public double hours { get; set; }
public string notes { get; set; }
public Project project { get; set; }
public object invoice { get; set; }
}
public class Root
{
public List<Entry> entries { get; set; }
}
When you deserialize the string, you'll get objects. You won't look through anything in the JSON string; you'll loop through elements of the List object.
For example:
MyLibrary.Root root = MyLibrary.MyClass::GetData(); System.IEnumerator entryEnumerator = root.entries.GetEnumerator(); while (entryEnumerator.MoveNext) { MyLibrary.Entry entry = entryEnumerator.Current; }
By the way, your code doesn't respect C# naming conventions. Property names (such as entries) should start with a capital letter.
Note that I thought you already have C# and you know how to use, that's why I suggested you could use it directly instead of rewriting it to X . If it's not the case, using X will probably be better for you.
I meant to say - How I can retrieve the values of the fields when we deserialize the string(response) and how can I loop through each elements in the Json?
I'm a bit confused. What do you mean by "to convert it in C#"? You used "to convert" when talking about rewriting C# to X++, but "converting it in C#" doesn't make sense in such a context. Do you mean how to deserialize JSON files in C#?
Thanks Sergei! Appreciate it!
Thanks Martin, Can you please help me with any sample code? Like I want to learn, how we can convert it in C# and loop/iterate to get the data?
Another approach is not converting it to X at all - you can call C# libraries from X , therefore your C# code could deserialize the file and simple return a Root object with the data, which you'll use in X for whatever you need.
By the way, please use Insert > Insert Code (in the rich formatting view) to paste source code. Look at the difference:
public class Project { public int id { get; set; } public string name { get; set; } } public class Entry { public double hours { get; set; } public string notes { get; set; } public Project project { get; set; } public object invoice { get; set; } } public class Root { public List entries { get; set; } }
Hi RahulD365,
You can take a look at method FormJsonSerializer::deserializeCollection and use cross-references to check how it's used for list object deserialization.
Steps for conversions
1. Create data contract classes in x++ for Project and Entity (docs.microsoft.com/.../walkthrough-exposing-an-x-class-as-a-data-contract )
2. Use FormJsonSerializer::deserializeCollection to deserialize json to list of Entry objects
The only field which can cause issues is Entry.invoice because of object type. Do you know what is stored in that field?
André Arnaud de Cal...
294,033
Super User 2025 Season 1
Martin Dráb
232,854
Most Valuable Professional
nmaenpaa
101,158
Moderator