[英]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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.