How to convert a table to json?

0

The table looks like this:

tab = { 
       semana={"seg","ter","qua"},
       mês={"jan","fev","mar"},
       modelo={
              casaco={"pele","couro"}
       }            
}

I need the output to be in json format for javascript to interpret.

saída:  [{ "semana":["seg","ter,"qua"],
           "mês":["jan","fev","mar"],
           "modelo": { "casaco":["pele","couro"]
           }
}] 
    
asked by anonymous 14.07.2017 / 16:45

1 answer

0

Using the library json.pris

The first line calls the library, the second one encodes the table for json and the json string, the third is an error handling.

local json = inclua 'json'            
cadeiajson, erros = json.cod(tab);   
se nao cadeia entao erro(erros) fim;  

The output from the table above would look like this:

imprima(cadeiajson)

{"modelo":{"casaco":["pele", "couro"]}, "mes":["jan", "fev", "mar"], "semana":["seg", "ter", "qua"]}
    
17.07.2017 / 15:55