SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2012
    Location
    India
    Posts
    73
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Cannot understand JSON code

    i haven't studied javascript and i am directly doing jquery, so i am not able to understand what this code tells us. please elaborate the following


    Q1.why value of var txt begin with single quote here? In JSON file i don't think so this is the syntax.

    Code:
    var txt = '{ "employees" : [' +
    '{ "firstName":"John" , "lastName":"Doe" },' +
    '{ "firstName":"Anna" , "lastName":"Smith" },' +
    '{ "firstName":"Peter" , "lastName":"Jones" } ]}';

    Q2.why in the value " + txt + " is present? is this showing blank space then txt and then again space?
    Code:
    var obj = eval ("(" + txt + ")");

  2. #2
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,313
    Mentioned
    178 Post(s)
    Tagged
    8 Thread(s)
    Hi there,

    Quote Originally Posted by moneysharma View Post
    Q1.why value of var txt begin with single quote here? In JSON file i don't think so this is the syntax.
    This doesn't really have much to do with JSON, rather with string concatenation.
    The txt variable is a string and begins with a single quote, so that the double quotes that occur within it don't have to be escaped.
    If you wrote it using double quotes, you would have to write:

    Code JavaScript:
    var txt = "{ \"employees\" : [" +

    and so on ...

    Q2.why in the value " + txt + " is present? is this showing blank space then txt and then again space?[/QUOTE]

    We have already established that txt is a string and again, this is an example of string concatenation.
    eval is passed a string as an argument, in this case a string consisting of a bracket, the variable txt and a closing bracket.

    If it makes it any easier to understand, you could rewrite your example as:

    Code JavaScript:
    part1 = "(";
    part2 = txt;
    part3 = ")"
    string = part1 + part2 + part3;
    eval(string);

    Best thing to do is read this:
    http://www.quirksmode.org/js/strings.html

    and pay special attention to this bit:
    http://www.quirksmode.org/js/strings.html#conc

  3. #3
    SitePoint Enthusiast
    Join Date
    Oct 2012
    Location
    India
    Posts
    73
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Pullo View Post
    Hi there,



    This doesn't really have much to do with JSON, rather with string concatenation.
    The txt variable is a string and begins with a single quote, so that the double quotes that occur within it don't have to be escaped.
    If you wrote it using double quotes, you would have to write:

    Code JavaScript:
    var txt = "{ \"employees\" : [" +

    and so on ...

    Q2.why in the value " + txt + " is present? is this showing blank space then txt and then again space?
    We have already established that txt is a string and again, this is an example of string concatenation.
    eval is passed a string as an argument, in this case a string consisting of a bracket, the variable txt and a closing bracket.

    If it makes it any easier to understand, you could rewrite your example as:

    Code JavaScript:
    part1 = "(";
    part2 = txt;
    part3 = ")"
    string = part1 + part2 + part3;
    eval(string);

    Best thing to do is read this:
    http://www.quirksmode.org/js/strings.html

    and pay special attention to this bit:
    http://www.quirksmode.org/js/strings.html#conc[/QUOTE]

    Perfect answer...thanks a ton!


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
  •