使用jquery或javascript添加或更改JSON键的值

时间:2022-08-23 08:09:52

I have a JSON string(?) that I have returned from $.ajax() and named it data. Some of the values are empty and I need to add values to some of the keys and send it back to my PHP script.

我有一个JSON字符串(?),我从$ .ajax()返回并命名为数据。有些值为空,我需要为某些键添加值并将其发送回我的PHP脚本。

I access the existing values by data.keyName. How do I add or change the values of certain keys in "data"?

我通过data.keyName访问现有值。如何在“数据”中添加或更改某些键的值?

This is what data looks like.

这就是数据的样子。

{
    "ID":"48",
    "userID":"0",
    "address":"750 North High Street",
    "city":"Columbus",
    "state":"OH",
    "zip":"43215",
    "lat":"39.977673",
    "lng":"-83.003357",
    "busNumber":"55",
    "isClaimed":"N",
    "whereFound":"",
    "busNum":"",
    "email":"",
    "fname":"",
    "lname":"",
    "comments":""
}  

6 个解决方案

#1


29  

Once you have decoded the JSON, the result is a JavaScript object. Just manipulate it as you would any other object. For example:

解码完JSON后,结果就是一个JavaScript对象。像对待任何其他对象一样操纵它。例如:

data.busNum = 12345;
...

#2


16  

var temp = data.oldKey; // or data['oldKey']
data.newKey = temp;
delete data.oldKey;

#3


2  

Just like you would for any other variable, you just set it

就像你对任何其他变量一样,你只需设置它

alert(data.ID);
data.ID = "bar";  //dot notation 
alert(data.ID);    
data.userID = 123456;
data["address"] = "123 some street"; //bracket notation

#4


1  

It seems if your key is saved in a variable. data.key = value won't work.

似乎您的密钥保存在变量中。 data.key = value不起作用。

You should use data[key] = value

您应该使用data [key] = value

Example:

例:

data = {key1:'v1', key2:'v2'};

var mykey = 'key1'; 
data.mykey = 'newv1';
data[mykey] = 'newV2';

console.log(data);

Result:

结果:

{
  "key1": "newV2",
  "key2": "v2",
  "mykey": "newv1"
}

#5


0  

data.userID = "10";

#6


0  

var y_axis_name=[];

 for(var point in jsonData[0].data)
              { 
                y_axis_name.push(point);

              }

y_axis_name is having all the key name

y_axis_name具有所有密钥名称

try on jsfiddle

试试jsfiddle

#1


29  

Once you have decoded the JSON, the result is a JavaScript object. Just manipulate it as you would any other object. For example:

解码完JSON后,结果就是一个JavaScript对象。像对待任何其他对象一样操纵它。例如:

data.busNum = 12345;
...

#2


16  

var temp = data.oldKey; // or data['oldKey']
data.newKey = temp;
delete data.oldKey;

#3


2  

Just like you would for any other variable, you just set it

就像你对任何其他变量一样,你只需设置它

alert(data.ID);
data.ID = "bar";  //dot notation 
alert(data.ID);    
data.userID = 123456;
data["address"] = "123 some street"; //bracket notation

#4


1  

It seems if your key is saved in a variable. data.key = value won't work.

似乎您的密钥保存在变量中。 data.key = value不起作用。

You should use data[key] = value

您应该使用data [key] = value

Example:

例:

data = {key1:'v1', key2:'v2'};

var mykey = 'key1'; 
data.mykey = 'newv1';
data[mykey] = 'newV2';

console.log(data);

Result:

结果:

{
  "key1": "newV2",
  "key2": "v2",
  "mykey": "newv1"
}

#5


0  

data.userID = "10";

#6


0  

var y_axis_name=[];

 for(var point in jsonData[0].data)
              { 
                y_axis_name.push(point);

              }

y_axis_name is having all the key name

y_axis_name具有所有密钥名称

try on jsfiddle

试试jsfiddle