The simplest way to get the JSON data length method jsonlist.length
The code is as follows |
Copy Code |
var str = ' [{"username": "Liu Xiaofei", "Bay": "2010/06/17"},{"username": "From Small P", "Bay": "2010/06/11"}] '; |
Alert (str.length) In fact, it outputs a result length of 2
Here's what I do with JavaScript code, why do I only provide JavaScript code? Since other languages have more or less provided some information about JSON, but JavaScript does not provide much, and then in the process of web development, now relies more on jquery, so don't say much, see code:
The code is as follows |
Copy Code |
function Getjsonobjlength (jsonobj) { var Length = 0; For (var item in jsonobj) { length++; } return Length; } |
The use method is also simple, we assume that there is a JSON object is "Jsontemp", the specific content is {["name": "John", "Age": 18],["name": "Dick", "age": 19]}. The only things we need to do when using JavaScript are the following:
The code is as follows |
Copy Code |
var count = getjsonobjlength (jsontemp);
|
To develop applications, such as the need to read each object in JSON in a traversal manner, follow these steps:
The code is as follows |
Copy Code |
For (I=0;i<getjsonobjlength (jsontemp); i++) { name = Jsontemp[i].name; age = Jsontemp[i].age; } |
Okay, here's the basic application.
Add
Discussion on the method of reading JSON in S
JS Way to read JSON I have two kinds of contacts:
Method One: Function construction definition method returns
The code is as follows |
Copy Code |
var Strjson = "{name: ' JSON name '}";//Get JSON var obj = new Function ("return" + Strjson) ()//converted JSON object alert (obj.name);//json name |
Method Two: The famous eval function in JS
The code is as follows |
Copy Code |
var Strjson = "{name: ' JSON name '}";//Get JSON var obj = eval ("(+ Strjson +)");//Converted JSON object alert (obj.name);//json name |
The second way to be aware of this is that the object expression {' name ': ' JSON name '} must be extended with ' () ' otherwise
The code is as follows |
Copy Code |
var Strjson = "{name: ' JSON name '}"; var obj = eval (Strjson); alert (obj.constructor);//string constructor alert (obj.name);//undefine |
An object expression must be extended to eval execution to generate an anonymous object!