1

Trying to get all JSON objects in an array. It's only returning the last one.

Here's a sample of my JSON:

{
    "manufacturer":{
        "name": "manufacturername",
        "cameras": [
            {
                "name": "sdfsdfsd",
                "type": "Audio device",
                "resolution": "Unknown",
                "channels": "1"
            }
        ]
    },
    "manufacturer":{ 
        "name": "manufacturername2",
        "cameras": [
        {
            "name": "sdfsdf",
            "type": "Camera",
            "resolution": "720P/1.3MP",
            "channels": "2"
        },
        {
            "name": "D12",
            "type": "Camera",
            "resolution": "1080P/3MP",
            "channels": "1"
        }
]
}}

It is valid JSON.

Here's how I'm calling it:

    //Get Manufacturer data
$http.get('data2.json').success(function(data) {

    $scope.maninfo = data;
    console.log($scope.maninfo);
});

The actual array is much longer - and it's just returning the last Object for some reason.

1
  • 1
    You want [] brackets around it, not {}. Commented Dec 3, 2013 at 20:27

1 Answer 1

4

What you have is valid JSON, but it doesn't correctly express your intent. "manufacturer" (or "name", or "cameras") isn't a type name, it's a unique key into a collection of named values -- dictionary, map, hash, whatever(1). JSON data structures are just a subset of JavaScript object literal declarations (hence the name: JavaScript Object Notation).

So the example above is not an array, it's two successive value assignments to the "manufacturer" property of the same parent object. The parser is assigning the first one to the "manufacturer" property, then replacing that with the second (and in your original, larger) "array", it's then replacing that with the third, and so on.

The "cameras" properties in the manfacturer objects are properly functioning arrays. Just do the same at the higher level -- something more like this:

{
    "manufacturers":
    [
        {
            "name": "manufacturername",
            "cameras": [
                {
                    "name": "sdfsdfsd",
                    "type": "Audio device",
                    "resolution": "Unknown",
                    "channels": "1"
                }
            ]
        },
        { 
            "name": "manufacturername2",
            "cameras": [
                {
                    "name": "sdfsdf",
                    "type": "Camera",
                    "resolution": "720P/1.3MP",
                    "channels": "2"
                },
                {
                    "name": "D12",
                    "type": "Camera",
                    "resolution": "1080P/3MP",
                    "channels": "1"
                }
            ]
        }
    ]
}

(1) Dictionary, map, hash -- or "associative array". But I didn't want to call it any kind of "array" in that paragraph, because the whole point is it's not the other kind of array.

Sign up to request clarification or add additional context in comments.

1 Comment

This was perfect. Thanks. I now also have a much better understanding of the structure of JSON depending on intent.

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.