How to convert a table to json?

Asked

Viewed 500 times

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"]
           }
}] 
  • how you got these values?

1 answer

0


Using the library json.pris

the first line calls the library, the second encodes the table for json and stores in cadeiajson, the third is an error handling.

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

The output of the table above would look like this:

imprima(cadeiajson)

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.