今天有同事问我关于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;
{
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();
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);
}
info(returnJsonstr);
}
二.D365中将json反序列化成实体类:
myentity entity;
list = FormJSONSerializer::deserializeCollection(classnum(List), json字符串,Types::Class,'myentity');
listEnumerator = list.getEnumerator();
listEnumerator = list.getEnumerator();
while (listEnumerator.moveNext())
{
entity= listEnumerator.current();
{
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;
}
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);
'{"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();
{
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"));
}
}
{
lineCon = listEnumerator.current();
lineMap = Map::create(lineCon);
info(lineMap.lookup("FieldName"));
info(lineMap.lookup("FieldValue"));
}
}