Você pode criar dados da mensagem JSON que contém objetos JSON, matrizes JSON, ou ambos, criando elementos na árvore de mensagens lógicas, sob o elemento Data que pertence à raiz do analisador JSON.
Uma mensagem JSON pode ter um objeto anônimo ou uma matriz anônima como raiz dos dados. Quando você cria uma matriz JSON na árvore de mensagens lógicas, o nome da matriz JSON é colocado em um elemento da árvore que tem um tipo configurado como o tipo de elemento do analisador JSON JSON.Array.
Os itens na matriz JSON são colocados na árvore lógica como elementos NameValue, como filhos do elemento JSON.Array, com o valor obrigatório. Os nomes desses elementos de item de matriz não são usados pelo serializador JSON porque os itens de matriz JSON são anônimos. Entretanto, para consistência com o nome que é usado pelo analisador JSON, use o nome Item quando definir os elementos de item de matriz.
O exemplo a seguir mostra como criar uma mensagem JSON que é formatada com um objeto no nível raiz, com o 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)
)
O exemplo a seguir mostra como criar uma mensagem que é formatada com uma matriz no nível raiz, no 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 obter mais informações sobre matrizes PHP, consulte Usando Matrizes PHP com JSON.
Message: ( ['json' : 0xc552990]
(0x01001000:Array ):Data = (
(0x03000000:NameValue):Item = 'valueA' (CHARACTER)
(0x03000000:NameValue):Item = 'valueB' (CHARACTER)
)
O exemplo a seguir mostra como criar uma mensagem que é formatada com uma matriz de objetos no nível raiz, no 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)
)
)
)