AXForum  
Вернуться   AXForum > Microsoft Dynamics AX > DAX Blogs
DAX
Забыли пароль?
Зарегистрироваться Правила Справка Пользователи Сообщения за день Поиск

 
 
Опции темы Поиск в этой теме Опции просмотра
Старый 23.12.2021, 05:30   #1  
Blog bot is offline
Blog bot
Участник
 
25,475 / 846 (79) +++++++
Регистрация: 28.10.2006
domhk: Taking JSON input with D365 service and deserialize into contract class
Источник: https://domhk.blogspot.com/2021/12/t...rvice-and.html
==============

Recently I wanted to create a custom serivce in D365 taking in a JSON input. I did my usual google search but ended up needing additional trial and error to complete the use case. Here I'll share the solution I have..
Firstly, create a contract class which represent the a Json object
///
/// The Student Information object contains the Student ID and Name
///
[DataContractAttribute]
class StudentInfoContract
{
str studentId;
str studentName;

[DataMemberAttribute("Student Id")]
public str parmStudentId(str _studentId = studentId)
{
studentId = _studentId;

return studentId;
}

[DataMemberAttribute("Student Name")]
public str parmStudentName(str _studentName = studentName)
{
studentName = _studentName;

return studentName;
}

}

Then, in the service contract class, define a List of Str which will take in the Json
///
/// Contains a list of students
///
[DataContractAttribute]
class StudentsContract
{
List studentList = new List(Types::String);

[
DataMemberAttribute("Student list"),
DataCollectionAttribute(Types::Class, classStr(StudentInfoContract))
]
public List parmStudentList(List _studentList = studentList)
{
if (!prmIsDefault(_studentList))
{
studentList = _studentList;
}

return studentList;
}

}

At run time, when calling code provides a list of Json objects, it'll be come a list of JObjects in X++. We'll loop through the list and use FormDeserializer to deserialize each JObject into a contract instance
List studentList = StudentsContract.parmStudentList();

ListEnumerator enumerator = studentList.getEnumerator();

while (enumerator.moveNext())
{
Newtonsoft.Json.Linq.JObject jObj = enumerator.current();

studentInfoContract = FormJsonSerializer::deserializeObject(classNum(StudentInfoContract),jObj.ToString());

str studentId, studentName;

studentId = studentInfoContract.parmStudentId();
studentName = studentInfoContract.parmStudentName();

Info(strFmt("Studnet # %1: %2", studentId, studentName));
}

In the end it's quite simple, and also no need to create C# class representing the data contract as well.

This posting is provided "AS IS" with no warranties, and confers no rights.


Источник: https://domhk.blogspot.com/2021/12/t...rvice-and.html
Теги
d365fo, json, интеграция

 

Похожие темы
Тема Автор Раздел Ответов Посл. сообщение
Navigate Into Success: Passing JSON from JavaScript to C/AL Blog bot Dynamics CRM: Blogs 0 31.12.2014 10:00
Navigate Into Success: Long time no see, Vienna, Nashville, demo gods, and other things Blog bot Dynamics CRM: Blogs 0 09.10.2013 01:15
Navigate Into Success: Web Reference vs. Service Reference, Part 3 Blog bot Dynamics CRM: Blogs 0 31.01.2012 04:44
Navigate Into Success: Web Reference vs. Service Reference, Part 2 Blog bot Dynamics CRM: Blogs 0 30.01.2012 11:04
Creating Custom Dynamics AX Services Blog bot DAX Blogs 0 17.12.2008 12:05

Ваши права в разделе
Вы не можете создавать новые темы
Вы не можете отвечать в темах
Вы не можете прикреплять вложения
Вы не можете редактировать свои сообщения

BB коды Вкл.
Смайлы Вкл.
[IMG] код Вкл.
HTML код Выкл.
Быстрый переход

Рейтинг@Mail.ru
Часовой пояс GMT +3, время: 10:05.
Powered by vBulletin® v3.8.5. Перевод: zCarot
Контактная информация, Реклама.