从原始字符串形成树或节点

时间:2017-07-23 17:37:14

标签: javascript java json

我有以下字符串

 var string = "@anno1[ data1 xyz @anno2[data2  @anno3[data3] data4] data5 @anno4[data6] data7]"

我想将其转换为如下对象:

var childs = [ 
        {
            name : '@anno1',
            text : 'data1 xyz data2 data3 data4 data5 data6 data7',
        }, 
        {
            name : '@anno2',
            text : 'data2 data3 data4'
        }, 
        {
            name : '@anno3',
            text : 'data3'
        }, 
        {
            name : '@anno4',
            text : 'data6'
        } 
]

我已经尝试了很多方法,但我没有成功,提前谢谢。

这是我试过的java代码

    import java.util.HashMap;
import java.util.Map;

public class ExpressionTree {

    static Map <String, String> resultMap = new HashMap<String, String>(16);

    public static void main(String args[]) {
        // String exp = "[data1 xyz @anno2[data2 @anno3[data3] data4] data5 @anno4[data6] data7]";

        String exp = "@anno1[ data1 xyz @anno2[data2  @anno3[data3] data4] data5 @anno4[data6] data7 ]";

        parseRecursively(exp);
    }

    private static void parseRecursively(String exp) {

        String annotation = null;
        String annotationValue = null;

        if (exp.startsWith("@")) {
            int dataStartIndex = exp.indexOf("[");
            annotation = exp.substring(0, dataStartIndex);
            annotationValue = exp.substring(0 + dataStartIndex, exp.lastIndexOf("]")+1).trim();

            System.out.println(annotation);
            System.out.println(annotationValue);

            resultMap.put(annotation, annotationValue);

            parseRecursively(annotationValue);

        } else {
            int annotationStartIndex = exp.indexOf("@");
            int dataStartIndex = exp.substring(1).indexOf("[");

            if( -1 != annotationStartIndex || -1 != dataStartIndex)
            {
                annotation = exp.substring(annotationStartIndex, dataStartIndex+1).trim();

                String nextData = exp.substring(0 + dataStartIndex + 1).trim();
                String tmpNextData = nextData;

                int countOfOpenBrackets = 0;
                for (int i = 0; tmpNextData.charAt(i) != ']'; i++) {
                    // System.out.println(tmpNextData.charAt(i));
                    if (tmpNextData.charAt(i) == '[') {
                        countOfOpenBrackets++;
                    }
                    // tmpNextData = tmpNextData.substring(1);
                }

                tmpNextData = nextData;
                int endIndexOfCurrentData = 0;

                for (int i = 0; i < tmpNextData.length() && endIndexOfCurrentData == 0; i++) {
                    // System.out.println(tmpNextData.charAt(i));
                    if (tmpNextData.charAt(i) == ']') {
                        countOfOpenBrackets--;
                    }
                    if (countOfOpenBrackets == 0) {
                        endIndexOfCurrentData = i + 1;
                    }
                    // tmpNextData = tmpNextData.substring(1);
                }

                annotationValue = nextData.substring(0, endIndexOfCurrentData).trim();

                System.out.println(annotation);
                System.out.println(annotationValue);

                resultMap.put(annotation, annotationValue);

                parseRecursively(annotationValue);
            }
        }
        System.out.println(resultMap);
    }
}

1 个答案:

答案 0 :(得分:1)

这是一个JavaScript(ES6)解决方案:

&#13;
&#13;
function toObject(string) {
    "use strict";
    const tokens = string.match(/[\[\]]|[^\s\[\]]+/g),
        result = [];
    let i = 0;
    function recurse() {
        const words = [];
        let obj;
        while (i < tokens.length) {
            let token = tokens[i];
            if (token[0] === '@') {
                obj = {name: token};
                result.push(obj);
            } else if (token === '[') {
                i++;
                obj.text = recurse();
                words.push(obj.text);
            } else if (token === ']') {
                break;
            } else {
                words.push(token);
            }
            i++;
        }
        return words.join(' ');
    }
    recurse();
    return result;
}

const string = "@anno1[ data1 xyz @anno2[data2  @anno3[data3] data4] data5 @anno4[data6] data7]";
const result = toObject(string);
console.log(result);
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;
&#13;
&#13;