Puede crear datos de mensaje JSON que contengan objetos JSON, matrices JSON, o ambos, creando elementos en el árbol lógico de mensaje, bajo el elemento Data, que es propiedad de la raíz del analizador JSON.
Un mensaje JSON puede tener un objeto anónimo o una matriz anónima como raíz de los datos. Al crear una matriz JSON en el árbol lógico de mensaje, el nombre de matriz JSON se coloca en un elemento del árbol que tiene un tipo que está establecido en el tipo de elemento del analizador JSON JSON.Array.
Los elementos de la matriz JSON se colocan en el árbol lógico como elementos NameValue, como hijos del elemento JSON.Array, con el valor necesario. Los nombres de estos elementos de matriz no se utilizan en el serializador JSON, ya que los elementos de la matriz JSON son anónimos. Sin embargo, por motivos de coherencia con el nombre que utiliza el analizador JSON, utilice el nombre Item para definir los elementos de matriz.
El ejemplo siguiente muestra cómo crear un mensaje JSON que se formatea con un objeto en el nivel raíz, con el formato { --- }.
{"Message":"Hello World"}
SET OutputRoot.JSON.Data.Message = 'Hello World';
MbElement outRoot = outMessage.getRootElement();
MbElement outJsonRoot =
outRoot.createElementAsLastChild(MbJSON.PARSER_NAME);
MbElement outJsonData =
outJsonRoot.createElementAsLastChild(MbElement.TYPE_NAME, MbJSON.DATA_ELEMENT_NAME, null);
MbElement outJsonTest = outJsonData.createElementAsLastChild(MbElement.TYPE_NAME_VALUE, "Message", "Hello World");
$output_message->{MB_JSON_PARSER_NAME}->{MB_JSON_DATA_ELEMENT_NAME}->Message = 'Hello World';
Message: ( ['json' : 0xc552990]
(0x01000000:Object ):Data = (
(0x03000000:NameValue):Message = 'Hello World' (CHARACTER)
)
El ejemplo siguiente muestra cómo crear un mensaje que se formatea con una matriz a nivel raíz, en el formato [ -- ].
["valueA","valueB"]
CREATE FIELD OutputRoot.JSON.Data IDENTITY (JSON.Array)Data;
CREATE LASTCHILD OF OutputRoot.JSON.Data TYPE NameValue NAME 'Item' VALUE 'valueA';
CREATE LASTCHILD OF OutputRoot.JSON.Data TYPE NameValue NAME 'Item' VALUE 'valueB';
MbElement outJsonRoot =
outRoot.createElementAsLastChild("JSON");
MbElement outJsonData =
outJsonRoot.createElementAsLastChild(MbJSON.ARRAY, "Data", null);
outJsonData.createElementAsLastChild(MbElement.TYPE_NAME_VALUE,
"Item", "valueA");
outJsonData.createElementAsLastChild(MbElement.TYPE_NAME_VALUE,
"Item", "valueB");
$output_assembly->JSON->Data[] = array("valueA","valueB");
Para obtener más información sobre matrices PHP, consulte el apartado Utilización de matrices PHP con JSON.
Message: ( ['json' : 0xc552990]
(0x01001000:Array ):Data = (
(0x03000000:NameValue):Item = 'valueA' (CHARACTER)
(0x03000000:NameValue):Item = 'valueB' (CHARACTER)
)
El ejemplo siguiente muestra cómo crear un mensaje que se formatea con una matriz de objetos al nivel de raíz, en el formato [{--},{--},...].
[{"Nam1":"val1","Num1":1},{"Nam2":"val2","Num2":2}]
CREATE FIELD OutputRoot.JSON.Data IDENTITY (JSON.Array)Data;
SET OutputRoot.JSON.Data.Item[1].Nam1 = 'val1';
SET OutputRoot.JSON.Data.Item[1].Num1 = 1;
SET OutputRoot.JSON.Data.Item[2].Nam2 = 'val2'
SET OutputRoot.JSON.Data.Item[2].Num2 = 2;
MbElement jsonData =
outMessage.getRootElement().createElementAsLastChild(MbJSON.PARSER_NAME).createElementAsLastChild
(MbJSON.ARRAY,MbJSON.DATA_ELEMENT_NAME, null);
MbElement jsonFirstArrayItem =
jsonData.createElementAsLastChild(MbElement.TYPE_NAME, MbJSON.ARRAY_ITEM_NAME, null);
jsonFirstArrayItem.createElementAsFirstChild(MbElement.TYPE_NAME_VALUE, "Nam1", "val1");
jsonFirstArrayItem.createElementAsLastChild(MbElement.TYPE_NAME_VALUE, "Num1", new Integer(1));
MbElement jsonSecondArrayItem =
jsonData.createElementAsLastChild(MbElement.TYPE_NAME, MbJSON.ARRAY_ITEM_NAME, null);
jsonSecondArrayItem.createElementAsFirstChild(MbElement.TYPE_NAME_VALUE, "Nam2", "val2");
jsonSecondArrayItem.createElementAsLastChild(MbElement.TYPE_NAME_VALUE, "Num2", new Integer(2));
Message: ( ['json' : 0xc673900]
(0x01001000:Array ):Data = (
(0x01000000:Object):Item = (
(0x03000000:NameValue):nam1 = 'val1' (CHARACTER)
(0x03000000:NameValue):num1 = 1 (INTEGER)
)(0x01000000:Object):Item = (
(0x03000000:NameValue):nam2 = 'val2' (CHARACTER)
(0x03000000:NameValue):num2 = 2 (INTEGER)
)
)
)