transform js variable into json object [closed]

-1

Have the variable below:

[object Object]
  '0%' : [{ 'margin-left':'-0%'}],
  '25%' : [{ 'margin-left':'-0%'}],
  '30%' : [{ 'margin-left':'-100%'}],
  '50%' : [{ 'margin-left':'-100%'}],
  '55%' : [{ 'margin-left':'-200%'}],
  '75%' : [{ 'margin-left':'-200%'}],
  '80%' : [{ 'margin-left':'-300%'}],
  '100%' : [{ 'margin-left':'-300%'}],

Brought to you by

console.log(imagem);

How do you turn it into a json object?

Where does this come from?

  var tempoTransicao = 5;
  var quantasImagens = $("div.slider ul.slide li img").size();
  var tamanhoIntervalos = Math.round(100/quantasImagens);
  var tempoImagens = 0;
  var t = 0;    
  var imagem = "";  
  var imagens = {};

  for (i = 0; i < quantasImagens; i++) {    

      tMin = t + tempoTransicao;
      tMax = t + tamanhoIntervalos; 
      t+=tamanhoIntervalos;

      if(i==0) tMin=0;
      if(i==quantasImagens) tMax=100;         

      imagem += "'" + tMin + "%' : [{ 'margin-left':'-" + tempoImagens + "%'}],";
      imagem += "'" + tMax + "%' : [{ 'margin-left':'-" + tempoImagens + "%'}],";

      tempoImagens+=100;

  }

  $.keyframe.define([
        $.extend(
          { name: 'tocaSlide' }, 
          imagemjson
          )
  ]);

Any errors in this build?

var jsonString = "{ \"0%\" : { \"margin-left\":\"-0%\"},";
    jsonString += " \"25%\" : { \"margin-left\":\"-0%\"},";
    jsonString += " \"30%\" : { \"margin-left\":\"-100%\"},";
    jsonString += " \"50%\" : { \"margin-left\":\"-100%\"},";
    jsonString += " \"55%\" : { \"margin-left\":\"-200%\"},";
    jsonString += " \"75%\" : { \"margin-left\":\"-200%\"},";
    jsonString += " \"80%\" : { \"margin-left\":\"-300%\"},";
    jsonString += " \"100%\" : { \"margin-left\":\"-300%\"}}";

    jsonString = JSON.parse(jsonString);

console.log(jsonString);

$.keyframe.define([
      $.extend(
        { name: 'jsonString' }, 
           jsonString
        )
]);
    
asked by anonymous 29.10.2017 / 16:04

1 answer

1

Just use the parse () example:

var jsonString = "{ \"0\" : { \"margin-left\":\"-0%\"} }";
jsonString = JSON.parse(jsonString);
    
29.10.2017 / 16:07