SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Guru
    Join Date
    Jun 2009
    Posts
    820
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    JSON: check to see if value is string or object...

    you can't check JSON for whether or not they're strings like regular variables?

    this is not working for me...

    Code:
     if (typeof propName === 'string') {
    in this example,
    http://mayacove.com/dev/json/users.html

    to grab for the "joined" property (which contains three values, so had to loop again...)

    I had to "cheat" and do


    Code:
     if(PropName === "joined") {

    but the line I really wanted to use, namely

    Code:
     if (typeof propName === 'string') {
    was ignored.....

    but in my real situation need very much to detect whether a value is a string or an object... I assume this is pretty standard JSON stuff.....

    (in my real situation I need to check the Value, not the Prop name... and it's the exact same problem......

    this is ignored.....

    Code:
     if (typeof propValue === 'string') {
    )

    thank you....

  2. #2
    SitePoint Guru
    Join Date
    Jun 2009
    Posts
    820
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    actually I made a mistake in my post....

    the line I would have wanted to use here,
    http://mayacove.com/dev/json/users.html
    is

    Code:
     //	if (PropValue != 'string') {
    to detect if value for "joined" was a string or an object.....

    but, again, this line was ignored....

  3. #3
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,408
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    Quote Originally Posted by maya90 View Post
    the line I would have wanted to use here,
    http://mayacove.com/dev/json/users.html
    In your JS, you've got this line:
    Code JavaScript:
    if (PropValue typeOf != 'string')    //	this line is ignored....

    The typeof operator is all lower case, and needs to go before the value you're checking (as you've shown in previous posts). This should work:
    Code JavaScript:
    $.each(val, function(PropName,PropValue) {
        if (typeof PropValue != 'string') {
            console.log(PropValue + ' is NOT a string');
        }
    });


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •