Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

484
Vistas
QML and JSON: access to a field specified in a variable

I need to access to a Json string inside my qml code, now if the field is known is is easy.

Given {"c1":320, "c2":256, "c3":128}

I can

function getC1(jsonstr)
{
   const obj = JSON.parse(jsonstr);
   return obj.c1;
}

But what if I don't know in advance which field to access and have it in a variable.

like obj["c1"] or obj[variable] syntaxes are not working. Is there a way?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I disagree with your assertion that those other methods you tried don't work. Here is what I did:

    property string json: '{"c1":320, "c2":256, "c3":128}'

    function getC1(jsonstr)
    {
        const obj = JSON.parse(jsonstr);
        const key = "c1";
        console.log(obj.c1);
        console.log(obj["c1"]);
        console.log(obj[key]);
    }

    Component.onCompleted:
    {
        getC1(json);
    }

And here is the output:

qml: 320
qml: 320
qml: 320

So all 3 methods produce the desired output.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda