2016-12-01 63 views
0

我有一個硬編碼到我的頁面的JSON字符串,它用於我的應用程序中的設置。使用javascript修改JSON字符串

有一個用例,我需要爲這些設置添加一個選項,如果值爲true。

從我的psuedo代碼下面,我試圖添加一個「設置」的主要字符串,如果值爲true。

我試圖將JSON存儲爲一個數組,然後將新數據推入它,但java腳本抱怨它的格式不正確。

如何添加額外的json數據到我的主字符串?

var v = true, 
    test = { 
    "copySelected": { 
     "name": "Copy", 
     "icon": "fa-files-o" 
    }, 
    "sep1": "---------", 
    "success": { 
     "name": "Highlight: Green", 
     "icon": "fa-pencil" 
    }, 
    "info": { 
     "name": "Highlight: Blue", 
     "icon": "fa-pencil" 
    }, 
    "warning": { 
     "name": "Highlight: Yellow", 
     "icon": "fa-pencil" 
    }, 
    "danger": { 
     "name": "Highlight: Red", 
     "icon": "fa-pencil" 
    }, 
    "sep2": "---------", 
    "remove": { 
     "name": "Remove Highlight", 
     "icon": "fa-eraser" 
    }, 
    "sep3": "---------", 
    "addNote": { 
     "name": "Add Note", 
     "icon": "fa-file-text-o" 
    } 
} 

// I need to add this section to the above json at the end 
if(v){ 

    "sep4": "---------", 
    "removeUser": { 
     "name": "Remove User", 
     "icon": "fa-user-times" 
    } 

} 
+3

你不必JSON。 JSON是一種可以解析爲數據的文本格式。你有一個對象。因此,您可以像添加任何其他對象一樣爲其添加屬性。 –

回答

3

你顯示的只是一個普通的JavaScript對象。只是性質將其添加爲正常:

var v = true, 
 
    test = { 
 
    "copySelected": { 
 
     "name": "Copy", 
 
     "icon": "fa-files-o" 
 
    }, 
 
    "sep1": "---------", 
 
    "success": { 
 
     "name": "Highlight: Green", 
 
     "icon": "fa-pencil" 
 
    }, 
 
    "info": { 
 
     "name": "Highlight: Blue", 
 
     "icon": "fa-pencil" 
 
    }, 
 
    "warning": { 
 
     "name": "Highlight: Yellow", 
 
     "icon": "fa-pencil" 
 
    }, 
 
    "danger": { 
 
     "name": "Highlight: Red", 
 
     "icon": "fa-pencil" 
 
    }, 
 
    "sep2": "---------", 
 
    "remove": { 
 
     "name": "Remove Highlight", 
 
     "icon": "fa-eraser" 
 
    }, 
 
    "sep3": "---------", 
 
    "addNote": { 
 
     "name": "Add Note", 
 
     "icon": "fa-file-text-o" 
 
    } 
 
} 
 

 
// I need to add this section to the above json at the end 
 
if(v){ 
 

 
    test.sep4 = "---------"; 
 
    test.removeUser = { 
 
     name: "Remove User", 
 
     icon: "fa-user-times" 
 
    }; 
 

 
} 
 

 
console.log(test.sep4) 
 
console.log(test.removeUser);

如果你收到一個JSON字符串,那麼你只需撥打JSON.parse(string)和返回值是一個對象,你會那麼只需添加屬性如下所示。 (注:現在的對象封裝在報價和只是一個字符串)。

 var v = true, 
 
     test = `{ 
 
     "copySelected": { 
 
      "name": "Copy", 
 
      "icon": "fa-files-o" 
 
     }, 
 
     "sep1": "---------", 
 
     "success": { 
 
      "name": "Highlight: Green", 
 
      "icon": "fa-pencil" 
 
     }, 
 
     "info": { 
 
      "name": "Highlight: Blue", 
 
      "icon": "fa-pencil" 
 
     }, 
 
     "warning": { 
 
      "name": "Highlight: Yellow", 
 
      "icon": "fa-pencil" 
 
     }, 
 
     "danger": { 
 
      "name": "Highlight: Red", 
 
      "icon": "fa-pencil" 
 
     }, 
 
     "sep2": "---------", 
 
     "remove": { 
 
      "name": "Remove Highlight", 
 
      "icon": "fa-eraser" 
 
     }, 
 
     "sep3": "---------", 
 
     "addNote": { 
 
      "name": "Add Note", 
 
      "icon": "fa-file-text-o" 
 
     } 
 
    }` 
 
    
 
    var result = JSON.parse(test); 
 

 
    // I need to add this section to the above json at the end 
 
    if(v){ 
 

 
     result.sep4 = "---------"; 
 
     result.removeUser = { 
 
      name: "Remove User", 
 
      icon: "fa-user-times" 
 
     }; 
 

 
    } 
 

 
    console.log(result.sep4) 
 
    console.log(result.removeUser);