L. Sanna
L. Sanna

Reputation: 6552

why do javascript libraries using json choose a [ { } , { } ] structure

I've been using a few javascript libraries, and I noticed that most of them take input in this form: [{"foo": "bar", "12": "true"}]

According to json.org:

enter image description here

So we are sending an object in an array.

So I have a two part question:

Part 1: Why not just send an object or an array, which would seem simpler?

Part2: What is the best way to create such a Json with Php?

Here is a working method, but I found it a bit ugly as it does not work out of the box with multi-dimensional arrays:

    <?php 
$object[0] = array("foo" => "bar", 12 => true); 

$encoded_object = json_encode($object); 
?> 

output:

{"1": {"foo": "bar", "12": "true"}}

<?php $encoded = json_encode(array_values($object)); ?> 

output:

[{"foo": "bar", "12": "true"}]

Upvotes: 4

Views: 218

Answers (3)

starbeamrainbowlabs
starbeamrainbowlabs

Reputation: 6106

Question one:

JSON is just a way of representing an object and/or and array as a string. If you have your array or object as a string, it is much easier to send it around to different places, like to the client's browser. Different languages handle arrays and objects in different ways. So if you had an array in php, for example, you can't send it to the client directly because it is in a format that only php understands. This is why JSON is useful. It is a method of converting an array or object to a string that lots of different languages understand.

Question two:

To output an array of objects like the above, you could do this:

<?php
    //create a test array of objects
    $testarray = array();
    $testarray[] = json_decode('{"type":"apple", "number":4, "price":5}');
    $testarray[] = json_decode('{"type":"orange", "number":3, "price":8}');
    $testarray[] = json_decode('{"type":"banana", "number":8, "price":3}');
    $testarray[] = json_decode('{"type":"coconut", "number":2, "price":9}');

    $arraycount = count($testarray);


    echo("[");

    $i = 1;
    foreach($testarray as $object)
    {
        echo(json_encode($object));
        if($i !== $arraycount)
        {
            echo(",");
        }

        $i += 1;
    }

    echo("]");
?>

This will take an array of objects and loop over them. Before we loop over them, we output the opening square bracket.

Then, for each object in the array, we output it encoded to JSON plus a comma after each one. We count the number of iterations so that we don't output a comma at the end.

Upvotes: 0

deceze
deceze

Reputation: 522165

  1. Because that's the logical way how to pass multiple objects. It's probably made to facilitate this:

    [{"foo" : "bar", "12" : "true"}, {"foo" : "baz", "12" : "false"}]
    
  2. Use the same logical structure in PHP:

    echo json_encode(array(array("foo" => "bar", "12" => "true")));
    

Upvotes: 9

David Schwartz
David Schwartz

Reputation: 182779

An array is used as a convenient way to support multiple parameters. The first parameter, in this case, is an object.

Upvotes: 0

Related Questions