Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

206
Visualizações
How to parsing the correct values from a json decoding

Hii ha ve written this question because i have a json converted to a string with a function of php called json_encode()

Something like this:

{ "data":"2","state":"false"}

when the original json that im trying to encode is like this:

{ "data":2,"state":false}

(Please note that the type of the variables are different, in the original i have an int number called data and boolean called state, but when i use json_encode() every variable goes to a string..)

The problem is when i try to json.PARSE() the value from the json encode of php in angular i don´t get the correct value, every variable is a string...

For example, isntead of getting the boolean state of the variable, i get "false" or "true", and this is a problem...

There is a way to parse this avoiding this problem? basically my problem is when i parse the json in my angular project i dont get the correct type of variable..

Thanks!

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can use JSON.parse on an individual value to convert it into the desired type:

const oldValue = JSON.parse('{"data":"2","state":"false"}')

const result = {}

Object.keys(oldValue).forEach(key => {
  result[key] = JSON.parse(oldValue[key]);
});

Here's the other way around:

const oldValue = JSON.parse('{"data": 2,"state": false}');

const result = {}

Object.keys(oldValue).forEach(key => {
  result[key] = oldValue[key].toString();
});

Note: These examples assume flat objects. You'd need to recurse through the object's tree if some of the values are objects, for example {a: {b: "c"}}.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda