transform js variable into json object

Asked

Viewed 740 times

-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 by

console.log(imagem);

How to make it into a json object?

Where does that 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
          )
  ]);

Some mistake in this construction?

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
        )
]);
  • Can you show where this string comes from? Just like this has a syntax error because Javascript does not allow line breaks inside strings...

  • yes, at the end of the question. the goal is to generate the json obj in the picture variable.

  • Now I’m even more curious: which application is this? or better: what is the functionality you want to create?

  • then, I want to use the function $.keyframe.define([, and put this json variable in as it is at the end of the question. The goal is to dynamically change a css keyframe

1 answer

1

Just use the function parse() example:

var jsonString = "{ \"0\" : { \"margin-left\":\"-0%\"} }";
jsonString = JSON.parse(jsonString);
  • error at position 0

  • Edited answer, you need to escape the quotes

  • So with a single line it worked, but I added at the end of the question the json complelo and it’s not working. Did I get the construction wrong?

  • in your case I think it would be better to build an object in javascript and convert to json, simple

Browser other questions tagged

You are not signed in. Login or sign up in order to post.