tengo este codigo:
let test = '{"attribute_as":"plan_id","operator":"fromTo","values":"{"from":"70","to":"80"}"}'; console.log(JSON.parse(test)); Por supuesto, falla porque en values tengo un objeto. ¿Hay alguna opción de cómo analizar esta cadena de manera fácil? ¿O no es posible en absoluto?
Al final el resultado debe ser:
{ attribute_as: 'plan_id', operator: 'fromTo', values: { from: 70, to: 80 } }La cadena es incorrecta:
let err = '{"attribute_as":"plan_id","operator":"fromTo","values":"{"from":"70","to":"80"}"}'; // This is the original string let pass = '{"attribute_as":"plan_id","operator":"fromTo","values":{"from":70,"to":80}}'; // Corrected string let desiredObj = { attribute_as: 'plan_id', operator: 'fromTo', values: { from: 70, to: 80 } }; console.log(JSON.stringify(desiredObj) == err); console.log(JSON.stringify(desiredObj) == pass);Esto debería funcionar. Cuando se registraron, ambos se evaluaron correctamente.
let test = '{"attribute_as": "plan_id","operator": "fromTo","values": {"from": 70,"to": 80}}', test2 = { attribute_as: 'plan_id', operator: 'fromTo', values: { from: 70, to: 80 } } console.log(JSON.parse(test)); console.log(JSON.stringify(test2));