Results 1 to 6 of 6

Thread: A doubt related to JSON arrays

  1. #1
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default A doubt related to JSON arrays

    Hi,

    Please have a look at the following code

    Code:
    function show() {
    		var columnHeaders = [
    		         	{key:"id_no"},
    			    ];
    		var keyValue = columnHeaders[0].key;
    		
                    var JSONArray2 = [{keyvalue:"testing"}];
    }
    In the above function what I am trying to achieve is to create a JSON array that will be like the following

    Code:
    JSONArray2 = [{"id_no":"testing"}]
    The question is how I can specify a key value using a variable name in which the original key value I want to use?

    Now below code

    Code:
    var keyValue = columnHeaders[0].key;
    		
    var JSONArray2 = [{keyvalue:"testing"}];
    gives the following

    Code:
    JSONArray2 = [{keyvalue:"testing"}]
    Please help me to understand this problem

    Regards

    CodeExploiter

  2. #2
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by codeexploiter View Post
    The question is how I can specify a key value using a variable name in which the original key value I want to use?
    Bracket notation:

    Code:
    object[variable] = value;
    For instance,

    Code:
    var keyValue = columnHeaders[0].key,
        jsonArray = [{}];
    
    jsonArray[0][keyValue] = 'testing';
    The property names in an object literals are themselves literals: either expressed as strings, numbers, or identifiers. However, the identifiers are not references to variables. Think instead of the identifier in variable declarations.

    Mike

  3. #3
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    Hi mike,

    Thanks a lot for the answer.

    Regards

    Codex

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    However, note that numerical arrays aren't a part of the JSON specification.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Twey View Post
    However, note that numerical arrays aren't a part of the JSON specification.
    Yes, they are, with regards to both Douglas Crockford's original description and RFC 4627 (also written by Crockford).

    Mike

  6. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Oh yes, so they are. My error.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

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
  •