简体   繁体   English

使用jquery替代JSON数组值

[英]Substitute JSON Array Value Using jquery

I've been trying for quite some time to get a JSON file and edit one of the values in the array (to be passed to PHP) 我已经尝试了相当长的时间来获取JSON文件并编辑数组中的值之一(以传递给PHP)

My current code is 我当前的代码是

j1 = $.getJSON("data.json") 
j1.responseJSON[name].job = "Carpenter"

When I use console.log(j1) after that, it logs the JSON the same as it was before (with name's job being “Baker”). 之后,当我使用console.log(j1) ,它将记录与以前相同的JSON(名称的工作为“ Baker”)。 What am I doing wrong, and how can I fix it? 我在做什么错,该如何解决?

UPDATE 1: ADDED JSON 更新1:添加JSON

{
     name:{
          {"job":"Baker", "age":"twenty-three","educated":"yes"}
     }
}

$.getJSON("data.json") is async. $.getJSON("data.json")是异步的。 You need a callback. 您需要一个回调。 Otherwise you set the value before the request is being done. 否则,请在完成请求之前设置该值。

http://api.jquery.com/jQuery.getJSON/ http://api.jquery.com/jQuery.getJSON/

$.getJSON("data.json", function(j1){
    j1.responseJSON[name].job = "Carpenter"
    console.log(j1)
})

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

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