使用jquery或javascript添加或更改JSON键的值 [英] Add or change a value of JSON key with jquery or javascript

查看:142
本文介绍了使用jquery或javascript添加或更改JSON键的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

这是数据的样子。

{
    "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":""
}  


推荐答案

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

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

data.busNum = 12345;
...

这篇关于使用jquery或javascript添加或更改JSON键的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆