Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

241
Views
Intentando analizar y acceder al elemento en un objeto JSON

Estoy tratando de analizar y acceder al elemento en un objeto JSON. ¿Cómo puedo acceder a la matriz de 'entidad' en mi carga útil JSON?

 var results = {"id": "92-4dac-4307-9038-c50e829a022a","funa": ["4667"],"entity": [{"id":"98","en": "com","type": "AAF"},{"id":"99","en": "com","type": "AAF"}]}; gs.log(">> raw result: " + results.funa); //output : 4667 var str = JSON.stringify(results); var parser = new JSONParser(); results = parser.parse(str); gs.log(" >> after parse mots: " + results.funa); //output : 4667 gs.log(" >> after parse id0: " + results.entity.length); //output : 2 for (var key in results) gs.log("Key is: " + key + " and value is: " + results.entity[key]); //undefined
about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Esta pregunta está etiquetada con "ServiceNow" y ninguna de las respuestas anteriores es sensible a los matices del motor Rhino de ServiceNow. La sintaxis de ES6 generará el siguiente error en cualquier Javascript compiler exception: syntax error . En la actualidad (enero de 2022), ServiceNow solo es compatible con ES5 JavaScript.

Debe usar el envoltorio JSON de ServiceNow para analizar y clasificar sus objetos. El código del envoltorio JSON vive en un Script Incluir llamado JSON que puede leer detenidamente.

Hay varios métodos ES5 disponibles para usted, incluido el método forEach() . Debido a que la entidad es una matriz, usamos forEach() para iterar sobre la matriz de objetos de la entidad. Tenga en cuenta que esta es solo una de varias formas de iterar sobre esta matriz.

Actualicé su fragmento de código y agregué comentarios para reflejar la respuesta a su pregunta.

 var results = { "id": "92-4dac-4307-9038-c50e829a022a", "funa": ["4667"], "entity": [ {"id":"98","en": "com","type": "AAF"}, {"id":"99","en": "com","type": "AAF"} ]}; gs.info(">> raw result: " + results.funa[0]); // output : 4667 var resultsStr = global.JSON.stringify(results); // Convert result to string var resultsObj = global.JSON.parse(resultsStr); // Convert resultStr to an object gs.info(" >> after parse mots: " + resultsObj.funa[0]); // output : 4667 gs.info(" >> after parse id0: " + resultsObj.entity.length); //output : 2 resultsObj.entity.forEach(function(entity) { // Use forEach() to iterate through each obj in the entity array for (var key in entity) { // Iterate through each key in the current array element gs.info("Key is: " + key + " and value is: " + entity[key]); // Access entity array element value from the parsed resultsObj } });
about 4 years ago · Santiago Trujillo Report

0

Puede usar el método forEach para iterar sobre la matriz de entidades. Luego, para iterar sobre las propiedades de cada elemento en la matriz de entidades, puede usar la sintaxis for (var key in item) .

 results.entity.forEach(item => { for (var key in item) { gs.log("Key is: " + key + " and value is: " + item[key]); } });

Creo que eso es lo que estás buscando.

about 4 years ago · Santiago Trujillo Report

0

No estoy seguro de haber entendido bien, pero ¿estás buscando algo como esto?

 var results = {"id": "92-4dac-4307-9038-c50e829a022a","funa": ["4667"],"entity": [{"id":"98","en": "com","type": "AAF"},{"id":"99","en": "com","type": "AAF"}]}; console.log("raw result: " + results.funa); //output : 4667 var str = JSON.stringify(results); results = JSON.parse(str); results.entity.forEach(entity => { Object.keys(entity).forEach(key => { console.log("Key is: " + key + " and value is: " + entity[key]) }) })

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!