buenas tardes :
necesito generar un archivo json con data anidada.
He usado la funcion hb_jsonEncode() pero solo logro un nivel.
hb_jsonEncode( {'Mensaje'=> {'Head' => Head }
Mensaje
Head
Body
Pedidos
Detalles
alguna sugerencia ?
Saludos
----------------------------
{
"Seguridad":
{
"compania": "ASA",
"password": "Y0b3lPrb ",
"usuario": "CRIRXUSR01"
}
,
"Mensaje": {
"Head":
{
"fecha_origen": "2025-05-09T15:21:01 ",
"id_mensaje": "00019727 ",
"sistema_origen": "MG ",
"tipo": "RECPED "
}
,
"Body": {
"Pedidos":
{
"PEDCCL": "20503141389 ",
"PEDCIA": "ASA",
"PEDFCH": "09/05/2025",
"PEDFPR": "2025-05-09",
"PEDNOC": "OCP-SA742-052509 ",
"PEDNRO": "P0001389-250509",
"PEDTDA": "ALMA01 ",
"PEDTIT": 1,
"PEDTUN": 12.0000
,
"Detalles":
[
{
"pedaux": " 1",
"pedcpr": "0007019 ",
"pedctd": " 12"
}
]
}
}
}
}
json anidado
- Antonio Linares
- Site Admin
- Posts: 44508
- Joined: Thu Oct 06, 2005 5:47 pm
- Location: Spain
- Has thanked: 717 times
- Been thanked: 311 times
- Contact:
Re: json anidado
Este código funciona bien aqui:
Code: Select all | Expand
function Main()
? hb_jsonEncode( GetJson() )
return nil
function GetJson()
local cText
TEXT INTO cText
{
"Seguridad":
{
"compania": "ASA",
"password": "Y0b3lPrb ",
"usuario": "CRIRXUSR01"
}
,
"Mensaje": {
"Head":
{
"fecha_origen": "2025-05-09T15:21:01 ",
"id_mensaje": "00019727 ",
"sistema_origen": "MG ",
"tipo": "RECPED "
}
,
"Body": {
"Pedidos":
{
"PEDCCL": "20503141389 ",
"PEDCIA": "ASA",
"PEDFCH": "09/05/2025",
"PEDFPR": "2025-05-09",
"PEDNOC": "OCP-SA742-052509 ",
"PEDNRO": "P0001389-250509",
"PEDTDA": "ALMA01 ",
"PEDTIT": 1,
"PEDTUN": 12.0000
,
"Detalles":
[
{
"pedaux": " 1",
"pedcpr": "0007019 ",
"pedctd": " 12"
}
]
}
}
}
}
ENDTEXT
return cText
Re: json anidado
Estimado Antonio :
Presumo que no me explique bien
tengo la siguiente data
// Seguridad
hHash := {=>}
hHash ["compania"] := "ASA"
hHash ["usuario"] := "CRIRXUSR01
hHash ["password"] := "ABCD1234"
Segu := hHash
// Head
hHash := {=>}
hHash ["id_mensaje"] := "00019727 "
hHash ["sistema_origen"] := "MG "
hHash ["fecha_origen"] := "2025-05-09T15:21:01 "
hHash ["tipo"] := "RECPED"
Head := hHash
// Pedido
hHash := {=>}
hHash ["PEDCIA"] := "ASA"
hHash ["PEDFPR"] := "2025-05-09"
hHash ["PEDNRO"] := "P0001389-250509"
hHash ["PEDCCL"] := "20503141389 "
cab := hHash
// Detalles
hHash ["PEDAUX"] := 1
hHash ["PEDCPR"] := "0007019'
hHash ["PEDCTD"] := 12
det := hHash
y lo que necesito es obtener el archivo JSON que tenga la estructura que detallo lineas abajo
Agradezco su apoyo.
Poli
------------
{
"Seguridad":
{
"compania": "ASA",
"password": "ABCD1234 ",
"usuario": "CRIRXUSR01"
}
,
"Mensaje": {
"Head":
{
"fecha_origen": "2025-05-09T15:21:01 ",
"id_mensaje": "00019727 ",
"sistema_origen": "MG ",
"tipo": "RECPED "
}
,
"Body": {
"Pedidos":
{
"PEDCCL": "20503141389 ",
"PEDCIA": "ASA",
"PEDFPR": "2025-05-09",
"PEDNRO": "P0001389-250509",
"Detalles":
[
{
"pedaux": " 1",
"pedcpr": "0007019 ",
"pedctd": " 12"
}
]
}
}
}
}
Presumo que no me explique bien
tengo la siguiente data
// Seguridad
hHash := {=>}
hHash ["compania"] := "ASA"
hHash ["usuario"] := "CRIRXUSR01
hHash ["password"] := "ABCD1234"
Segu := hHash
// Head
hHash := {=>}
hHash ["id_mensaje"] := "00019727 "
hHash ["sistema_origen"] := "MG "
hHash ["fecha_origen"] := "2025-05-09T15:21:01 "
hHash ["tipo"] := "RECPED"
Head := hHash
// Pedido
hHash := {=>}
hHash ["PEDCIA"] := "ASA"
hHash ["PEDFPR"] := "2025-05-09"
hHash ["PEDNRO"] := "P0001389-250509"
hHash ["PEDCCL"] := "20503141389 "
cab := hHash
// Detalles
hHash ["PEDAUX"] := 1
hHash ["PEDCPR"] := "0007019'
hHash ["PEDCTD"] := 12
det := hHash
y lo que necesito es obtener el archivo JSON que tenga la estructura que detallo lineas abajo
Agradezco su apoyo.
Poli
------------
{
"Seguridad":
{
"compania": "ASA",
"password": "ABCD1234 ",
"usuario": "CRIRXUSR01"
}
,
"Mensaje": {
"Head":
{
"fecha_origen": "2025-05-09T15:21:01 ",
"id_mensaje": "00019727 ",
"sistema_origen": "MG ",
"tipo": "RECPED "
}
,
"Body": {
"Pedidos":
{
"PEDCCL": "20503141389 ",
"PEDCIA": "ASA",
"PEDFPR": "2025-05-09",
"PEDNRO": "P0001389-250509",
"Detalles":
[
{
"pedaux": " 1",
"pedcpr": "0007019 ",
"pedctd": " 12"
}
]
}
}
}
}
- Otto
- Posts: 7020
- Joined: Fri Oct 07, 2005 7:07 pm
- Has thanked: 100 times
- Been thanked: 91 times
- Contact:
Re: json anidado
Hello, this is working for me.
Best regards,
Otto
Best regards,
Otto
Code: Select all | Expand
#include 'fivewin.ch'
#include "fileio.ch"
function Main()
local cJson := BuildJson()
? cJson
SaveJsonNoCRLF("test1.json",cJson)
return nil
function BuildJson()
local hSeguridad := {=>}
local hHead := {=>}
local hDet1 := {=>}
local hPedidos := {=>}
local hBody := {=>}
local hMensaje := {=>}
local hRoot := {=>}
local aDetalles := {}
hSeguridad["compania"] := "ASA"
hSeguridad["usuario"] := "CRIRXUSR01"
hSeguridad["password"] := "ABCD1234"
hHead["fecha_origen"] := "2025-05-09T15:21:01"
hHead["id_mensaje"] := "00019727"
hHead["sistema_origen"] := "MG"
hHead["tipo"] := "RECPED"
// Detalles: array of objects
hDet1["pedaux"] := 1
hDet1["pedcpr"] := "0007019"
hDet1["pedctd"] := 12
AAdd( aDetalles, hDet1 )
// Pedidos
hPedidos["PEDCCL"] := "20503141389"
hPedidos["PEDCIA"] := "ASA"
hPedidos["PEDFPR"] := "2025-05-09"
hPedidos["PEDNRO"] := "P0001389-250509"
hPedidos["Detalles"] := aDetalles
// Body
hBody["Pedidos"] := hPedidos
// Mensaje
hMensaje["Head"] := hHead
hMensaje["Body"] := hBody
// Root
hRoot["Seguridad"] := hSeguridad
hRoot["Mensaje"] := hMensaje
xbrowse(hRoot)
return hb_jsonEncode( hRoot )
FUNCTION SaveJsonNoCRLF( cFile, cJson )
LOCAL nH, cOut := cJson
// optional: auf UTF-8 ohne BOM umwandeln
IF hb_cdpSelect() != "UTF8"
cOut := hb_StrToUTF8( cOut )
ENDIF
// nur am Dateiende CR/LF wegtrimmen (falls vorhanden)
DO WHILE Len( cOut ) > 0 .AND. ( Right( cOut, 1 ) == Chr(13) .OR. Right( cOut, 1 ) == Chr(10) )
cOut := Left( cOut, Len( cOut ) - 1 )
ENDDO
nH := FCreate( cFile ) // neu anlegen (überschreibt)
IF nH < 0
RETURN .F.
ENDIF
FWrite( nH, cOut ) // schreibt exakt die Bytes, keine Ergänzungen
FClose( nH )
RETURN .T.