将点表示法转换为JSON [英] Converting Dot Notation to JSON

查看:120
本文介绍了将点表示法转换为JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堆形式的字符串:AAA.BBB [0] .CCC.DDD [5] .EEE = 123。有些甚至包含更深层次的嵌套数组。如何将该点符号转换为等效的JSON对象?我正在使用jQuery,如果它提供任何额外的优势。我已经找到了几乎可以做到这一点的方法,但是当包含数组时没有解决方案。

I have a bunch of strings of the form: "AAA.BBB[0].CCC.DDD[5].EEE = 123". Some contain even more deeply nested arrays. How would I convert that dot notation to the equivalent JSON object? I am using jQuery if that provides any additional advantages. I've found ways to ALMOST do this, but no solution works when arrays are included.

编辑:
我需要为许多字符串执行此操作并最终结合他们。特别是这个应该成为表格的对象(假设我没有犯任何错误):
{AAA:{BBB:[CCC:{DDD:[{} ,{},​​{},{},{},{EEE:123}]}]}

edit: I need to do this for many strings and ultimately combine them. This one, in particular, should become an object of the form (assuming I didn't make any errors): { "AAA" : { "BBB": [ "CCC : { "DDD": [ {}, {}, {}, {}, {}, { "EEE": 123 } ] } ] }

推荐答案

什么你所描述的不是只是转换为JSON的东西。你所描述的是对象的层次结构,当然,但是那里有那些括号,你显然在看但是,对于解析器将该字符串转换为javascript对象,我们可以这样做:

What you're describing isn't something that can just be converted to JSON. What you're describing is the hierarchy to the object, sure, but with those braces in there, you're obviously looking at but one piece of a much larger object. However, for a parser to turn that string into a javascript object, we can do this:

function splitStringToObject(string){
  var sourceArray = string.split('.');
  var top = {};
  var point = top;
  while (sourceArray.length){
    var work = sourceArray.shift();
    if ( /([a-zA-Z_][a-zA-Z0-9_]*)\[([a-zA-Z0-9_]+)\]/.test(work) ){
      console.log('found match alpha index')
      //found an array identifier with a variable name inside ('bbb[aaa]')
      var matches = /([a-zA-Z_][a-zA-Z0-9_]*)\[([a-zA-Z0-9_]+)\]/.exec(work);
      var matchName = matches[1];
      var matchIndex = matches[2];
      point[matchName] = [];
      point[matchName][matchIndex] = {};
      point = point[matchName][matchIndex];
    } else if ( /([a-zA-Z_][a-zA-Z0-9_]*)\[([0-9]+)\]/.test(work) ) {
      console.log('found match numeric index')
      //found an array identifier with a numeric index inside ('bbb[0]')
      var matches = /([a-zA-Z_][a-zA-Z0-9_]*)\[([a-zA-Z0-9_]+)\]/.exec(work);
      var matchName = matches[1];
      var matchIndex = matches[2];
      point[matchName] = [];
      point[matchName][matchIndex] = {};
      point = point[matchName][matchIndex];
    } else if ( work.indexOf('[') > -1 || work.indexOf(']') > -1 ) {
      console.log('found bad egg with ' + work)
    } else if ( work.indexOf('=') > 0 ) { 
      console.log('found = inside')
      //test for equals sign in the string
      var morework = work.split('=');
      work = morework[0].trim();
      sourceArray.push('='+morework[1])
      point[work] = morework[1].trim();
      point = point[work];
    } else { 
      console.log('found plain word')
      //assume work is aok to use as another object here.
      work = work.trim();
      point[work] = {};
      point = point[work];
    }
  }
  //you may not want this next part
  var ret;
  //let's pull our value off the top, altho if we do this, I don't know
  //how to retain the name. I prefer to return it under top still
  for (var k in top){
    ret = top[k];
  }

  console.log(ret);
  return ret;

  // alternately call
  return top;
}

然而,这只是一个解析器,我们如何使用它?好吧,我们需要提供我们的字符串。我假设你可以把你所有的字符串整齐地放到一个数组中,如下所示:

However, this is just a parser, how do we use that? Well, we need to feed it our strings. I'm going to assume you can neatly get all your strings into an array, like this:

var strings = [
  "AAA.BBB[0].CCC.DDD[1].EEE = 123",
  "AAA.BBB[0].CCC.DDD[2].EEE = 123",
  "AAA.BBB[0].CCC.DDD[4].EEE = 123",
  "AAA.BBB[0].CCC.DDD[5].EEE = 123",
];

然后下一部分变得非常简单,我们在这里看到:

and then the next part becomes really easy, as we see here:

var objectsConverted = [];
for(var k in strings){
  objectsConverted[k] = splitStringToObject(strings[k]);
}

var result = {};
for(var k in objectsConverted){
  jQuery.extend(true,result,objectsConverted[k]);
}

console.log(result);

如果我JSON.stringify(结果)我得到的结果是:

and at the end of the day if I JSON.stringify(result) I get:

"{"BBB":[{"CCC":{"DDD":[null,{"EEE":"123"},{"EEE":"123"},null,{"EEE":"123"},{"EEE":"123"}]}}]}"

这篇关于将点表示法转换为JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆