JSON-related knowledge, reprint: Delete array Delete in JSON

Source: Internet
Author: User

One: what is JSON

Jsong format: An object is a collection of unordered name/value pairs.

The object begins with parentheses and the parentheses end.

The name colon-delimited value.

"Name/value" separated by commas

Cases:

var people = {      "programmers": [{          "firstName": "Brett",          "lastName": "McLaughlin",          "email": "aaaa"      }, {          "firstName": "Jason",          "lastName": "Hunter",          "email": "bbbb"      }, {          "firstName": "Elliotte",          "lastName": "Harold",          "email": "cccc"      }] }Access data: people.programmers[0].lastname; Modify data: People.programmers[0].lastname= "423nfns"----------------------------- ------------

Two: The JSON method

Json.parse () and Json.stringify () method differences

Parse is used to parse a string into a JSON-formatted object.

The stringify is used to convert a JSON-formatted object into a Jsong-formatted string.

Example var student = new Object ();

Student.name = "Lanny"; Student.age = "25"; student.location = "China"; var json = json.stringify (student); alert (JSON); alert (student);

The result is: {"name": "Lanny", "Age": "+", "Location": "Chia"}

Alert (Student)
The result is: [Object Object]
--------------------------------------------------------------------------------------------------------------- ---------------------------------
Three: Application of JSON format delete data: reprint: http://xulavigne.blog.163.com/blog/static/196705036201211220250176/To delete the JSON element, first to understand the JSON data format, The Json,json in JavaScript is made up of objects and arrays, and the JSON data can be deleted as long as the object and array deletions in JavaScript are learned. 3 ways to delete an array from JavaScript (1) Shift () methodshift is to delete the value of the first item in the array, and returns the value of the deleted element, or undefined if the array is empty. var array=[1,9,6,3,8,5];var m=array.shift ()-->1console.log (array)-->9,6,3,8,5 2. Pop () methodThe pop method is used to delete the last item in the array, and returns the deleted value, and returns undefined if the array is empty. var array=[1,9,6,3,8,5];var m=array.pop ()-->5console.log (array)-->1,9,6,3,8 3. Splice () method can be added, deleted, replaced on an arrayvar array=[1,9,6,3,8,5];var m=array.splice (2,1,3,4)-->6console.log (array)-->1, 9, 3, 4, 3, 8, 5 note:The number of items to be replaced does not have to be equal to the number of items replaced, 1 items can be replaced by 3 items, 5 items can also be replaced with 3 items, which can be used to add and delete operations to an array. Delete:var array=[1,2,3,4,5];Array.splice (2,0,8,9);--->[]Console.log (Array)-->[1, 2, 8, 9, 3, 4, 5]Other non-reliable array deletion methodsThe method is not reliable, because there is no real deletion of the array item, but the position of its location is set to empty, the position of the item is still, the number of array items unchanged. unreliable Array removal Method 1:var array=[1,2,3,4,5];Array[4]=null;Console.log (Array)-->[1, 2, 3, 4, NULL]unreliable Array removal Method 2:var array=[1,2,3,4,5];d elete array[4];array-->[1, 2, 3, 4, Undefined]array.length--->5 JavaScript Delete Object Method js Delete the object delete operator var p={"name": "Kcscs", "Age": "Sex": "Female"}; Delete P.namefor (var i in P) {console.log (i);} The output age,sex,name item has been deleted the following JavaScript removes the JSON element to understand the JS delete object and array methods, the JSON data is manipulated by Var computer={//this JSON data has an array and object structure consisting of "CPU": [ "Intel", "AMD", "harddisk": ["Western Digital", "Seagete"], "motherboard": ["ASUS", "MSI"]}; If we want to remove the inter element inside the CPU, because this is an entry in the array, we use the method in the array computer.cpu.splice (0,1); Console.log (COMPUTER.CPU)-->["AMD"] , Inter was deleted if we want to delete harddisk, because he is an item in the object, so we want to use the object's Delete method to operate the delete computer.harddiskfor (var i in computer) { Console.log (i);} --Output CPU Motherboard,harddisk is removed Summary: The main data structure of JSON is object and array, we can effectively delete the elements in JSON as long as we have the corresponding operation methods . reproduced from the front-end development

JSON-related knowledge, reprint: Delete array Delete in JSON

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.