The use of json in javascript finishing

1, javascript to convert a string into a json object, you need to use jquery.js

<script type="text/javascript" src="jquery-1.8.1. Js"></script>
<script>
Var jsonStr1="{\"code\":\"001\",\"name\":\"hello1\"}";
Var jsonStr2="[{\"code\":\"002\",\"name\":\"hello2\"}]";
Var jsonStr3="[{\"code\":\"003\",\"name\":\"hello3\"},{\"code\":\"004\",\"name\": \"hello4\"}]";
// The first way to write, starting with jQuery 3.0, it is not recommended to use $.parseJSON. To parse a JSON string, use the native JSON.parse method instead.
Var jsonObj1=$.parseJSON(jsonStr1);
Var jsonObj2=$.parseJSON(jsonStr2);
Var jsonObj3=$.parseJSON(jsonStr3);
/ / The second way of writing
//var jsonObj1=JSON.parse(jsonStr1);
//var jsonObj2=JSON.parse(jsonStr2);
//var jsonObj3=JSON.parse(jsonStr3);
//The third way of writing
//var jsonObj1=eval('(' + jsonStr1 + ')');
//var jsonObj2=eval('(' + jsonStr2 + ')');
//var jsonObj3=eval('(' + jsonStr3 + ')');

Alert(jsonObj1.name);
Alert(jsonObj2[0].name);
Alert(jsonObj3[1].name);
</script>

Incoming JSON strings with incorrect formatting may cause an exception to be thrown. For example, the following invalid JSON strings:
{test: 1} (test does not use double quotes).
{'test': 1} (for 'test' Single quotes instead of double quotes).
"{test: 1}" (test does not use double quotes).
"{'test': 1}" ( 'test' uses single quotes instead of double quotes.)
"'test'" ('test' replaces double quotes with single quotes).
".1" ( Number must start with a number; "0.1" will be valid).
|"undefined" (undefined cannot represent a JSON string; however null, can).
"NaN" (NaN can't represent a JSON string; using Infinity to directly represent infinity is not allowed.)

2, json object is converted to string

<script>
var obj=new Object();
obj.code="001";
obj.name="hello1";
var str=JSON.stringify(obj);
alert(str);
</script>

3, get the number of json object entries, under normal circumstances you can use obj .length to get the quantity, if you want to get the number of object elements, you can use the following method

<script>
        function getJsonObjLength(jsonObj) {
            var Length = 0;
            for (var item in jsonObj) {
                Length++;
            }
            return Length;
        }
</script>

4, loop operation json

<script>
Var myObj = { "code":"001", "name":"hello1" };
/ / Use for-in to loop the properties of the object
For (x in myObj) {
    Alert(x);
}
//When for-in loops the properties of an object, use the brackets ([]) to access the value of the property.
For (x in myObj) {
    Alert(myObj[x]);
}
</script>

5, delete the properties of the JSON object

<script>
Var myObj, i, x = "";
myObj = {
    "name": "hello1",
    "alexa": 10000,
    "sites": {
        "site1": "123",
        "site2": "456",
        "site3": "789"
    }
}
/ / Use the delete keyword to delete the properties of the JSON object
Delete myObj.sites.site1;
/ / Use the brackets ([]) to delete the properties of the JSON object:
Delete myObj.sites["site2"];

For (i in myObj.sites) {
    x += myObj.sites[i] + "#";
}
Alert(x);
</script>