Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove the brackets in json?

Tags:

json

php

How can I get rid of the brackets below for json processing?

[{"success":true,"filename":"bialding_and_rebialding_plymouth02.jpg"},{"success":true,"filename":"bialding_and_rebialding_plymouth03.jpg"},{"success":true,"filename":"bialding_and_rebialding_plymouth04.jpg"}]

The result above is processed by the class below into an array,

function handle_upload($upload_directory)
    {
        # Loop the code according to the number of files.
        for($i = 1; $i <= $this->total; $i++)
        {
            ...

            if ($this->file->save($upload_directory.$name_filtered.'.'.$file_extension , $i-1))
            {
                $message[] = array('success'=>true,'filename'=>$name_filtered.'.'.$file_extension);
            }
            else 
            {
                $message[] = array('error'=> 'Could not save uploaded file.' . 'The upload was cancelled, or server error encountered');
            }
        }

        return $message;
    }

Then I use json_encode to turn the array into json format,

$uploader = new uploader();
$result = $uploader->handle_upload('uploads/');

echo htmlspecialchars(json_encode($result), ENT_NOQUOTES);

But I only need this in my result without the brackets,

{"success":true,"filename":"bialding_and_rebialding_plymouth02.jpg"},{"success":true,"filename":"bialding_and_rebialding_plymouth03.jpg"},{"success":true,"filename":"bialding_and_rebialding_plymouth04.jpg"}
like image 671
Run Avatar asked Aug 18 '11 14:08

Run


People also ask

What is [] and {} in JSON?

' { } ' used for Object and ' [] ' is used for Array in json.

What are the brackets in JSON?

Curly brackets {} delimit the beginning and end of a JSON object, therefore they are always found as the very first and last lines of a request. A JSON object contains one or more key-value pairs, where each pair is separated from the next by a comma, except for the last pair.

Is {} a valid JSON?

Is {} a valid JSON? At the time of writing, JSON was solely described in RFC4627. It describes (at the start of “2”) a JSON text as being a serialized object or array. This means that only {} and [] are valid, complete JSON strings in parsers and stringifiers which adhere to that standard.

Why does JSON have square brackets?

JSON Syntax Rules Curly braces organize objects, and the colon isolates each name. Square brackets hold the array, and commas separate values.


1 Answers

str_replace(array('[', ']'), '', htmlspecialchars(json_encode($result), ENT_NOQUOTES)); ?

like image 171
S M Avatar answered Sep 20 '22 11:09

S M