最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Find length of json string - Stack Overflow

programmeradmin0浏览0评论

I have following Jsonstring

  var j = { "name": "John" };
            alert(j.length);

it alerts : undefined, How can i find the length of json Array object??

Thanks

I have following Jsonstring

  var j = { "name": "John" };
            alert(j.length);

it alerts : undefined, How can i find the length of json Array object??

Thanks

Share Improve this question edited May 18, 2012 at 7:09 Bergi 665k161 gold badges1k silver badges1.5k bronze badges asked May 18, 2012 at 7:06 ghanshyam.miranighanshyam.mirani 3,10111 gold badges49 silver badges85 bronze badges 3
  • That isn't a "json Array object", it's a string literal that creates an object. – RobG Commented May 18, 2012 at 7:12
  • isn't it considered a json object? – Ryan Commented May 18, 2012 at 7:12
  • @RPM—No. JSON is "JavaScript Object Notation" that is based on ECMAScript object literal notation. – RobG Commented May 18, 2012 at 7:13
Add a comment  | 

5 Answers 5

Reset to default 8

Lets start with the json string:

var jsonString = '{"name":"John"}';

you can easily determine its length:

alert("The string has "+jsonString.length+" characters"); // will alert 15

Then parse it to an object:

var jsonObject = JSON.parse(jsonString);

A JavaScript Object is not an Array and has no length. If you want to know how many properties it has, you will need to count them:

var propertyNames = Object.keys(jsonObject);
alert("There are "+propertyNames.length+" properties in the object"); // will alert 1

If Object.keys, the function to get an Array with the (own) property names from an Object, is not available in your environment (older browsers etc.), you will need to count manually:

var props = 0;
for (var key in jsonObject) {
    // if (j.hasOwnProperty(k))
    /* is only needed when your object would inherit other enumerable
       properties from a prototype object */
        props++;
}
alert("Iterated over "+props+" properties"); // will alert 1

Another way of doing this is to use the later JSON.stringify method which will give you an object (a string) on which you can use the length property:

var x = JSON.stringify({ "name" : "John" });

alert(x.length);

Working Example

function getObjectSize(o) {
  var c = 0;
  for (var k in o) 
    if (o.hasOwnProperty(k)) ++c;
  return c;
}  
var j = { "name": "John" };

alert(getObjectSize(j)); // 1

There is no json Array object in javascrit. j is just an object in javascript.

If you means the number of properties the object has(exclude the prototype's), you could count it by the below way:

  var length = 0;
  for (var k in j) {
    if (j.hasOwnProperty(k)) {
      length++;
    }
  }
  alert(length);

An alternate in Jquery:

 var myObject = {"jsonObj" : [    
        {
            "content" : [
                {"name" : "John"},
            ]
        }
        ]
       }
        $.each(myObject.jsonObj, function() {
           alert(this.content.length);
        }); 

DEMO

发布评论

评论列表(0)

  1. 暂无评论