2016-12-01 34 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);