zoukankan      html  css  js  c++  java
  • 关于D365/AX2012/C#中的那些json、对象、字符串类型间的转换

    今天有同事问我关于AX2012里,如何直接输出json字符串的问题,这里就顺手写点东西跟大家分享下。

    一.AX2012中将字段信息序列化成json字符串:

    1.1.AX2012中将字段信息序列化成json字符串截图;

     1.2.关键代码:

    static void Alfred_Field2Json(Args _args)
    {
        System.IO.StringWriter stringWriter;
        Newtonsoft.Json.JsonTextWriter jsonWriter;
        str     returnJsonstr, res;
        stringWriter = new System.IO.StringWriter();
        jsonWriter = new Newtonsoft.Json.JsonTextWriter(stringWriter);
           
        jsonWriter.WriteStartObject();
        jsonWriter.WritePropertyName("FieldId1");
        jsonWriter.WriteValue("FieldValue1");
        jsonWriter.WritePropertyName('FieldId2');
        jsonWriter.WriteValue("FieldValue2");
        jsonWriter.WritePropertyName("FieldId3");
        jsonWriter.WriteValue("FieldValue3");
        jsonWriter.WriteEndObject();
        returnJsonstr = stringWriter.ToString();
        info(returnJsonstr);
    }
    二.D365中将json反序列化成实体类:
    myentity entity;
    list = FormJSONSerializer::deserializeCollection(classnum(List), json字符串,Types::Class,'myentity');
     listEnumerator = list.getEnumerator();
     while (listEnumerator.moveNext())
      {
              entity= listEnumerator.current();
      }
    三.D365中,将实体对象序列化成json字符串;
    FormJSONSerializer::serializeClass(实体对象);
    该类FormJSONSerializer可以实现对象和json的序列化与反序列化处理。
     
    四.D365/AX2012 提供接口,一次传多条记录;
    [AifCollectionTypeAttribute('return', Types::Class, classStr(DownFileValuelEntity))]
        public List<DownFileValuelEntity> downloadDN(FielEntity _fielEntity)
        {
            DownFileValuelEntity  valueEntity = new DownFileValuelEntity();
            List valueEntities = new List(types::Class);
          
            valueEntities.addEnd(valueEntity);
             
            return valueEntities;
        }
    五.D365/AX2012通过map反序列化;
     
        str jsonstr ='{"FieldValues":[{"FieldName":"Field1","FieldValue":"Rec1Field1Value"},'+
        '{"FieldName":"Field2","FieldValue":"Rec1Field2Value"}]}';
        Map     jsonMap, lineMap;
        container   lineCon;
        container   itemsCon;
        ListEnumerator          listEnumerator;
        mapEnumerator           mapEnumerator;
        List                    lineList;
        ;
        jsonMap = RetailCommonWebAPI::getMapFromJsonString(jsonstr);
        if (jsonMap.exists("FieldValues"))
        {
            itemsCon = jsonMap.lookup("FieldValues");
            lineList = List::create(itemsCon);
            listEnumerator = lineList.getEnumerator();
            while (listEnumerator.moveNext())
            {
                lineCon = listEnumerator.current();
                lineMap = Map::create(lineCon);
                info(lineMap.lookup("FieldName"));
                info(lineMap.lookup("FieldValue"));
            }
        }
  • 相关阅读:
    distcc加速内核编译
    ssh不检查server变化
    bbb u-boot SPI 启动
    Debian NAT共享上网
    Debian Epson L455 打印机
    Learn CMake's Scripting Language in 15 Minutes (ZZ)
    网络启动并安装Debian
    GNU LD 脚本学习笔记
    JLink defective
    获取真实mac地址
  • 原文地址:https://www.cnblogs.com/alfred-cn/p/13027843.html
Copyright © 2011-2022 走看看