简体   繁体   English

使用Javascript将嵌套密钥添加到JSON

[英]Adding Nested Key To JSON Using Javascript

I'm just starting to learn Javascript so my apologies if this is a stupid question. 我刚刚开始学习Javascript,因此如果这是一个愚蠢的问题,我深表歉意。

Basically my issue is this: 基本上我的问题是这样的:

Via an external form that I cannot change, I am receiving the following data. 通过无法更改的外部格式,我收到以下数据。

var data = "{\"status\": \"created\", \"name\": \"mike\", \"roleName\": \"Signer\", \"emailSubject\": \"test\", \"email\": \"hellO@yahoo.com\", \"templateId\": \"0171502E-38F7-43A7-BA09-6FC1FDAB09C2\" }"

Before I can send the data via API, the documentation requires that I add an additional nested key (templateRoles) to wrap rolename, emailsubject, and email into a new array. 在通过API发送数据之前,文档要求我添加一个附加的嵌套键(templateRoles),以将角色名,emailsubject和email包装到一个新数组中。 The final results is suppose to look like this. 最终结果应该看起来像这样。

var data = "{\"status\": \"created\", \"name\": \"mike\", \"templateRoles\": [{ \"roleName\": \"Signer\", \"emailSubject\": \"test\", \"email\": \"hellO@yahoo.com\"}], \"templateId\": \"0171502E-38F7-43A7-BA09-6FC1FDAB09C2\"}"

For the life of me I cannot figure out how to do this. 对于我的一生,我不知道该怎么做。 I've tried using splice and replace but either I am doing it wrong or that isn't the right approach. 我试过使用拼接和替换,但是我做错了或者这不是正确的方法。 Any suggestions or pointers in the right direction would be greatly appreciated. 任何建议或正确方向的指点将不胜感激。

Thanks in advance. 提前致谢。

Use JSON.parse(data) to convert your JSON string into an object. 使用JSON.parse(data)将JSON字符串转换为对象。 Then you can manipulate that object. 然后,您可以操纵该对象。 Once you're done manipulating simply use JSON.stringify() to get a JSON string back. 完成操作后,只需使用JSON.stringify()即可返回JSON字符串。

Sample code: 样例代码:

var data = "{\"status\": \"created\", \"name\": \"mike\", \"templateId\": \"0171502E-38F7-43A7-BA09-6FC1FDAB09C2\", \"templateRoles\": [{ \"roleName\": \"Signer\", \"emailSubject\": \"test\", \"email\": \"hellO@yahoo.com\"}]}";

var jsonObj = JSON.parse(data);
jsonObj.templateRoles = "some roles here";

var newDataStr = JSON.stringify(jsonObj);

So your basic code would look like this. 因此您的基本代码将如下所示。 Make the JSON string a JavaScript object, then manipulate it as such. 使JSON字符串成为JavaScript对象,然后对其进行处理。 Afterwards you can use JSON.stringyfi to put back to a JSON string. 之后,您可以使用JSON.stringyfi放回JSON字符串。

var jsonData = "{\"status\": \"created\", \"name\": \"mike\", \"templateId\": \"0171502E-38F7-43A7-BA09-6FC1FDAB09C2\", \"roleName\": \"Signer\", \"emailSubject\": \"test\", \"email\": \"hellO@yahoo.com\"}";

var parsedJson = JSON.parse(jsonData);

// now manipulate

parsedJson.newProperty = "hey there";

parsedJson.status = "not created";

As a side note, I don't think you can order the keys, so you can do whatever in terms of adding properties or functions. 附带说明一下,我认为您无法订购键,因此您可以在添加属性或功能方面做任何事情。 Never rely on the keys to be in order. 永远不要依赖于按键。 And by key, I mean property of function name of the object. 所谓键,是指对象的函数名称的属性。 Objects are held in a dictionary type way where their keys hold information. 对象以字典类型的方式保存,其中键保存信息。

So: 所以:

parsedJson["created"]

Is the same as 是相同的

parsedJson.created

You can conditionally add data depending on whether a key exists: 您可以根据密钥是否存在来有条件地添加数据:

for (var key in parsedJson)
    if (key === "someKey")
        // do something

OR 要么

if (parsedJson["someKey"] === undefined)
    // do something

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM