JS Gets the example of the length of the JSON object data

Source: Internet
Author: User
Tags eval

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!

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.