Thursday, June 19, 2025

Create API JSON Using Newtonsoft dll file on AX 2012 R3 CU8

 CLASS METHOD:

static str bodyJsonTransferSKN(    Amount amtValuex=0,

str currencyx='',

                                    str beneficiaryAccNox='',                                    

                                    str remarx='',

                                    str sourceAccountNox='', 

                                    str senderAccountNox='',

                                    str senderNamex=''

                               )

{

    str bodyRawJson;

    Counter                                 test;

    Newtonsoft.Json.Linq.JTokenWriter       writer;

    Newtonsoft.Json.Linq.JObject            jObject;

    ClrObject                               clrObject;

    TextIo                                  textIo;

    str                                     jsonStr, strDate, strTime;


    str amtValueStrx;

    str transDateStrx;


    ;


    amtValueStrx    = num2str(amtValuex,1,2,1,0);

    transDateStrx   = MyClass::getDateTimeZone();


    writer = new Newtonsoft.Json.Linq.JTokenWriter();


    writer.WriteStartObject();


    writer.WritePropertyName("sourceAccountNo");

    writer.WriteValue(sourceAccountNox);


    writer.WritePropertyName("beneficiaryAccountNo");

    writer.WriteValue(beneficiaryAccNox);


    writer.WritePropertyName("amount");


    writer.WriteStartObject();


        writer.WritePropertyName("value");

        writer.WriteValue(amtValueStrx);


        writer.WritePropertyName("currency");

        writer.WriteValue(currencyx);


    writer.WriteEndObject();


    writer.WritePropertyName("transactionDate");

    writer.WriteValue(transDateStrx);


    writer.WritePropertyName("remark");

    writer.WriteValue(remarx);


    writer.WritePropertyName("additionalInfo");


    writer.WriteStartObject();

       

        writer.WritePropertyName("senderAccountNo");

        writer.WriteValue(senderAccountNox);


        writer.WritePropertyName("senderName");

        writer.WriteValue(senderNamex);


    writer.WriteEndObject();


    writer.WriteEndObject();


    clrObject   = writer.get_Token();

    jObject     = clrObject;


    jsonStr     = jObject.ToString();


    bodyRawJson = jsonStr;


    return bodyRawJson;

}

CLASS METHOD:

static str getDateTimeZone()

{

    str         transDateStrx;

    utcDateTime systemDateTimex, currentDateTimex;

    Timezone    tzUserx;

    int         diffTimeMinutex, diffTimeHourx;


    ;


    systemDateTimex     = DateTimeUtil::getSystemDateTime();

    tzUserx             = DateTimeUtil::getUserPreferredTimeZone();


    diffTimeMinutex     = DateTimeUtil::getTimeZoneOffset(currentDateTimex, tzUserx);

    diffTimeHourx       = diffTimeMinutex/60;


    currentDateTimex    = DateTimeUtil::applyTimeZoneOffset(systemDateTimex, tzUserx);

    transDateStrx       = DateTimeUtil::toStr(currentDateTimex);


    transDateStrx       = strfmt('%1+0%2:00', transDateStrx, diffTimeHourx) ;


    return transDateStrx;

}


RESULT:

json result: 

{

  "sourceAccountNo": "",

  "beneficiaryAccountNo": "",

  "amount": {

    "value": "250000.00",

    "currency": "AAA"

  },

  "transactionDate": "2025-06-20T11:03:44+07:00",

  "remark": "",

  "additionalInfo": {

    "senderAccountNo": "",

    "senderName": ""

  }

}


No comments:

Post a Comment