Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用JQuery或Javascript操作JSON_Javascript_Jquery_Json - Fatal编程技术网

使用JQuery或Javascript操作JSON

使用JQuery或Javascript操作JSON,javascript,jquery,json,Javascript,Jquery,Json,我有一些JSON,我需要使用Javascript或JQuery来操作,这就是目前结构的外观 { "phoneNumbers": [ { "type": "home", "number": "212 555-1234" }, { "type": "fax", "number": "646 555-4567" } ] } 但

我有一些JSON,我需要使用Javascript或JQuery来操作,这就是目前结构的外观

{
    "phoneNumbers": [
        {
            "type": "home",
            "number": "212 555-1234"
        },
        {
            "type": "fax",
            "number": "646 555-4567"
        }
    ]
}
但是,我只需要方括号之间的位

谁能给我指出正确的方向,去掉方括号内的部分,包括括号本身


非常感谢。

如果这是一个来自服务器响应的字符串,或者其他什么,在JavaScript中,首先需要将JSON字符串解析为JSON对象。如果您使用jQuery,请使用$.parseJSON(您的JSON字符串),也可以在纯JavaScript中使用JSON.parse(您的JSON字符串),Chrome、Firefox等现代浏览器都支持此API

var my_json = '{"phoneNumbers":[{"type":"home","number":"212 555-1234"},{"type":"fax","number":"646 555-4567"}]}';
var my_object = JSON.parse(my_json);
var the_thing_i_care_about = my_object.phoneNumbers
那么操纵JSON对象就非常容易了。例如:

var phoneNumbers = $.parseJSON(your JSON string);
console.log(phoneNumbers[0].type);// get the first number type
phoneNumbers[0].type = "new type";// update the first number type "home" to "new type"
最后,我想您需要将JSON对象解析为字符串以发布到服务器

var phoneNumbersJSONString = JSON.stringify(phoneNumbers); // pure JavaScript API;
要支持所有浏览器,请选中此项


非常简单,对吧?

你知道如何用JavaScript解析JSON吗?您知道如何在JavaScript中获取对象的属性吗?