Add-delete and convert JSON object elements __js

Source: Internet
Author: User
Adds a deletion element-------a JSON object and a JSON array------------------------------------------------------






(1)
Plain JSON text can be like this


var json={
ID: "ID1",
Name: "Happy"
};

Add to
json["gender"]= "male"
json["Age"]=26;
alert (json.age);

Delete
Delete json["age"];
alert (json.age);
alert (Json.gender);












Removal of normal objects:
var o = new Object;
O.name = "David";
alert (o.name); Output "David"
Delete O.name;
alert (o.name); Output "undefined"






(2)
Pure JSON object Then just put the above var json first json1 = Json.parse (JSON) is OK














(3)
If you use JSON array text, you can do this.


var Mainarray = [
{"tag": "Task", "num": 2},
{"tag": "Work", "num": 6}
];

Mainarray. Push ({
"Tag": "Meeting", "num": 3
});




Mainarray.shift ()//delete Array header element
Mainarray.pop ()//delete array tail element











----the way to create a JSON text format by object------------------------------------------------------------------------------------




<p id= "a" >ssss</p>
<script>
var data = new Object ();
Data.name = "Airuikun";
Data.sex = "Man";
var s = json.stringify (data);
document.getElementById ("a"). InnerHTML = S;
</script>






















Json.parse (data)----converting from a JSON string to a JSON object
$.parsejson (data)----converting from a JSON string to a JSON object
Json.stringify ()----Convert a JSON object to a JSON string











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.