-1

Trying to identify the type of array, I have an multidimensional JSON array that i pull from a website, but its not using keypairs i am farmiliar with. The structure is as follows. I have only included 1 array item to show what it's doing.

"o": {
     "ah": ["id1", "12", "id2", "32", "id4", "4", "id5, "6"]
},

My searches on both JavaScript and JSON objects and strings always use a semi-colon : to define the key and the value.

In the end I just want to loop through the multiple items and print them out.

To provide more clarification about the array structure:

{
    "outer": {
            "item1":[ {
                "c": {
                    "k": 26862, "n": "theName"
                },
                "o": {
                    "ah": ["id1", "0", "id2", "0", "id3", "0.98", "id4", "0.94", "id5", "5", "id6", "-0/0.5"], 
                    "ou": ["id7", "3.5", "id8", "3.5", "id9", "1.53", "id10", "0.55", "id11", "3.4", "id12"],
                    "1x2": ["id13", "1.20", "id14", "25.00", "id15", "4.80"]
                    },
                    "egn":""
                }
                ]
            },
            {
            "item1":[ {
                "c": {
                    "k": 26862, "n": "theName"
                },
                "o": {
                    "ah": ["bd1", "0", "bd2", "0", "bd3", "0.98", "bd4", "0.94", "bd5", "0.5", "bd6", "-0.5"], 
                "ou": ["bd7", "3.5", "bd8", "3.5", "bd9", "1.53", "bd10", "0.55", "bd11", "3.4", "bd12"],
                "1x2": ["bd13", "1.20", "bd14", "25.00", "bd15", "4.80"]
                },
                "egn":""
                }
                ]
    } 
}

If I wanted to bind an ID to a specific field e.g. and it display the value of ID 1 in the array structure how would I do it without a keypair value, also if the array ID is not standardized so you can't loop through like var = "id"+ i foreach element?

3
  • 1
    This is a key/value pair: the first key ("o") has an object as a value. The second key ("ah"), inside that object, has an array as a value. Commented Aug 29, 2016 at 2:10
  • What do you mean by multidimensional? Are there multiple arrays? Can you show a more complete structure with three or more? Commented Aug 29, 2016 at 2:17
  • 1
    Please educate yourself on what "JSON" is. It's a string representation used for exchanging data, such as between a server and a client. What you are showing has nothing to do with JSON. It's just a JavaScript object. Anyway, if your objective is to "loop through" the object, why does your title say "object definition"? Commented Aug 29, 2016 at 2:52

1 Answer 1

5

o is an object, ah is an array which is a property of o object. You use JSON.parse() to convert JSON string to javascript object, then iterate over o.ah using Array.prototype.forEach()

// or however you obtain the JSON string
var obj = JSON.parse(jsonStr); 
obj.o.ah.forEach(function(value) {
  console.log(value);
});
Sign up to request clarification or add additional context in comments.

7 Comments

Wait, why the JSON.parse? o.ah is an array, not a JSON string.
@qxz "i have an multidimentional JSON array " If JSON.parse() is not necessary, o.ah.forEach(function) can be used
According to the JSON he posted, o.ah is an array... so the JSON.parse will cause an error.
@qxz "According to the JSON he posted, o.ah is an array." Yes, though if o is JSON, o is a string, not a javascript object?
Then you would have to do JSON.parse(o).ah.forEach. If o is a string, o.ah is undefined, because strings don't have an ah property
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.