0

我有一个存储为字符串的名称值对,我想将其转换为 jQuery 或 JavaScript 中的对象。这是那对的一个例子。

{'auto':false, 'itemAWidth':20, 'itemBWidth':20, 'marginLeft':0, 'marginRight':0, 'maximumWidth':0, 'propW':5, 'propX':5, ' propY':空,'propZ':空}

这被存储为一个字符串。如何将其转换为正确的对象?我的一些不成功的尝试如下所示。

convertValuePairsToObject = function (pairs) {

  var outObject = {};
  var nodes = pairs.split(',');
  for (var node in nodes) {

  }

  //alert(pairs);
  //return jQuery.parseJSON('"' + pairs + '"');
  //      var outObject = {};
  //      //var nodes = pairs.split(','), dest = outObject;
  //      var  = pairs.split(',');
  //      return outObject;
}

当我尝试使用 jQuery.parseJSON() 函数时,代码返回了一个非常长的字符串,以这样的开头。

{'0':'{','1':''','2':'0','3':''','4':':','5':''',' 6':'{', '7':''', '8':',', '9':'', '10':''', '11':'1', '12': ''', '13':':', '14':''', '15':''', '16':''', '17':',', '18':'' , '19':''', '20':'2', '21':''', '22':':', '23':''', '24':'a', ' 25':''', '26':',', '27':'', '28':''', '29':'3', '30':''','31':':', '32':''', '33':'u', '34':''', '35':',', '36':'', '37' :''', '38':'4', '39':''', '40':':', '41':''', '42':'t', '43':' '', '44':',', '45':' ', '46':''', '47':'5', '48':''', '49':':', '50':''', '51':'o',41':''', '42':'t', '43':''', '44':',', '45':'', '46':''', '47': '5', '48':''', '49':':', '50':''', '51':'o',41':''', '42':'t', '43':''', '44':',', '45':'', '46':''', '47': '5', '48':''', '49':':', '50':''', '51':'o',

======================================

在 2013 年 1 月 4 日上午 10:51 以下编辑

======================================

所以我尝试了你们俩提供的建议,并得到了这个。

  convertObjectToString = function (obj) {
      return JSON.stringify(obj);
      //      var str = '';
      //      for (var p in obj) {
      //         if (obj.hasOwnProperty(p)) { str += "'" + p + "':" + formatValue(obj[p]) + ", "; }
      //      }
      //      if (str.length > 0) {
      //         str = String(str).substring(0, str.length - 2); // Trim trailing comma
      //         str = '{' + str + '}';
      //      }
      //      return str;
   }

   convertValuePairsToObject = function (pairs) {
      return jQuery.parseJSON('[' + pairs + ']');
   }

但是现在当执行 convertValuePairsToObject 时,代码会在名称值对前面加上:

{“0”:

如果我反复触发该函数,它将继续将上述内容预先添加到字符串中,如下所示:

{"0":{"0":{"0":

我不需要索引标识符为零。我怎样才能消除它?

4

2 回答 2

3

您需要用双引号替换单引号:

var json = "{'auto':false, 'itemAWidth':20, 'itemBWidth':20, 'marginLeft':0, 'marginRight':0, 'maximumWidth':0, 'propW':5, 'propX':5, 'propY':null, 'propZ':null}".replace(/'/g, '"')

然后,您可以使用JSON.parse

JSON.parse(json)
于 2013-01-14T16:50:46.917 回答
0

假设你的意思是你得到这样的字符串:

"{'auto':false, 'itemAWidth':20, 'itemBWidth':20, 'marginLeft':0, 'marginRight':0, 'maximumWidth':0, 'propW':5, 'propX':5, 'propY':null, 'propZ':null}"

然后一种选择是eval像这样使用:

var pairs = "{'auto':false, 'itemAWidth':20, 'itemBWidth':20, 'marginLeft':0, 'marginRight':0, 'maximumWidth':0, 'propW':5, 'propX':5, 'propY':null, 'propZ':null}"

var foo = eval("(" + pairs + ")");

foo现在是实际对象而不是字符串。

执行console.log(foo);将导致调试器中的对象树不是字符串。


DEMO - 从字符串中获取对象


或者,您可以将字符串中的对象转换为正确的 JSON 对象,然后使用JSON.Parse其他人提到的方法转换回对象。

于 2013-01-14T16:51:11.393 回答