Metadata of JSON object in javascript

Discussion in 'JavaScript' started by knapsack3, Oct 10, 2007.

  1. #1
    Hi,

    I have a JSON object whose structure is like this:

    The name of the object is 'Emp' and the attributes are:
    name
    age
    sex
    department
    address

    I need to access the attribute names (not values) in javascript. Pls suggest a method for this.
     
    knapsack3, Oct 10, 2007 IP
  2. knapsack3

    knapsack3 Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    A correction :
    "I need to access" -> I need to obtain the attribute names in a local variable
     
    knapsack3, Oct 10, 2007 IP
  3. knapsack3

    knapsack3 Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I thought I should be eloborate in providing the requirement.

    Assume my JSON object is like ths:

    The name of the object is 'Emp'

    The attributes are:
    name
    age
    sex
    department
    address

    Later in my javascript code, I have to check if the JSON object's value matches with my local value. For this I have to check

    if (Emp.name == localvar.name) ...
    if (Emp.age == localvar.age) ...

    But the problem is I cannot know which of the attributes will come first for comparison, as no predefined order is allowed.
    So I have to first find out the current attribute of 'Emp' and accordingly compare the local variable with it.
    The problem is I cann't find the current attribute. Can someone pls tell me a way.

    Thanks.
     
    knapsack3, Oct 10, 2007 IP