将嵌套字符串转换为JSON对象javascript
convert a nested string into JSON object javascript
我有一个类似的嵌套查询字符串
var str = "( ( Sentence starts with any of null AND Sentence starts with any of null ) AND Sentence starts with any of null )"
如何使用javascript将其从AND运算符拆分为JSON对象,该对象看起来像:
{
"group":{
"operator":"AND",
"rules":[
{
"group":{
"operator":"AND",
"rules":[
object1,
object2
]
}
},
object3
]
}
}
在一般意义上,直到你的答案被清除:
var string = "my cool string AND I love JS AND isn't this cool?";
var operations = string.split(' AND '); // Gives an array: ["my cool string", "I love JS", "isn't this cool?"]
var group = {};
for(var i = 0; i < operations.length; ++i) {
operations['operator' + i] = operations[i];
}
console.log(group); // { operator1: "my cool string", operator2: "I love JS", operator3: "isn't this cool?"}
console.log(JSON.stringify(group)); // gives JSON string representation of group
相关文章:
- jQuery匹配JSON对象的部分文本
- 如何在Javascript中将JSon对象转换为数组
- 我可以在json对象中添加一个函数吗
- 使用JS将数组转换为json对象
- 如何为json对象中的段发送array[]
- 将JSON对象传递给angular指令
- 更改JSON对象的结构
- 访问JSON对象内部的数组元素
- 在ejs-partial中对JSON对象进行迭代
- 遍历 JSON 对象并检查 URL 是否以某个值结尾
- 访问嵌套JSON对象的键,其中键是动态的
- json对象中缺少对象循环
- 发送json对象或使用express路由呈现视图
- 在play2框架中向json对象添加下拉列表项
- 元素名称上带有短划线 (-) 字符的 Json 对象
- autocomplete不接受源的json对象
- 如何在javascript中创建动态json对象
- 在使用客户端脚本时拾取JSON对象
- 如何通过json对象数组为嵌套对象赋值
- 构造JSON对象