-1

This is my JavaScript object:

{
    "A":{
        "B":[{
            "C":{
                "D":[{
                    "test" : "Hello"
                }]
            }
        }]
    }
}

From this how do I store objects B and C in a variable?

  • var variable1 = response.A.B, var variable2 = variable1[0].c – kp singh Mar 03 '16 at 06:23
  • 1
    This is not JSON. It's a JavaScript object. `B` and `C` are not objects. They are **properties**. Also, what do you mean by "print object"? –  Mar 03 '16 at 06:35
  • Accessing an object's properties is about as basic as using the plus sign to add two numbers. I suggest you go back and bone up on your JS basics. For property access, you could start off here: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Property_accessors. –  Mar 03 '16 at 06:44

4 Answers4

0

If ob is the object representation of the JSON, ob.A.B should give you B. And notice that ob.A.B is an array containing one object with key C. Hence C can be accessed as ob.A.B[0].C, the first element of the array.

a_pradhan
  • 3,285
  • 1
  • 18
  • 23
0

Should be as simple as

 var a = {
"A":{
  "B":[{
    "C":{
      "D":[{
        "test" : "Hello"
      }]
    }
  }]
}
};

console.log( a.A.B ); //print the value of B
console.log( a.A.B[0].C ); //print the value of C
gurvinder372
  • 66,980
  • 10
  • 72
  • 94
-1

suppose we are storing your json into a variable data

data.A.B will give you Object B

and

data.A.B[0].C will give you Object C
Anoop LL
  • 1,548
  • 2
  • 21
  • 32
-1

Since b is an array : console.log(obj.A.B[0].C)

Renuka Deshmukh
  • 998
  • 9
  • 16