Viewed   74 times

I've been using json_encode for a long time, and I've not had any problems so far. Now I'm working with a upload script and I try to return some JSON data after file upload.

I have the following code:

print_r($result); // <-- This is an associative array
echo json_encode($result); // <-- this returns valid JSON

This gives me the following results:

// print_r result
Array
(
    [logo_url] => http://mysite.com/uploads/gallery/7f/3b/f65ab8165d_logo.jpeg
    [img_id] => 54
    [feedback] => Array
        (
            [message] => File uploaded
            [success] => 1
        )

)

// Echo result
{"logo_url":"http://mysite.com/uploads/gallery/7f/3b/f65ab8165d_logo.jpeg","img_id":"54","feedback":{"message":"File uploaded","success":true}}

Can anyone tell me why json_encode adds slashes?

update

@Quentin said that something is happening between json_encode and .parseJSON and he's right.

Doing a alert(data.toSource()); gives me the dollowing result:

({response:"{"logo_url":"http:\/\/storelocator.com\/wp-content\/uploads\/gallery\/7f\/3b\/71b9520cfc91a90afbdbbfc9d2b2239b_logo.jpeg","img_id":"62","feedback":{"message":"File uploaded","success":true}}", status:200})

And this is not valid JSON. It also adds the status:200 and I have no idea where this comes from.

Could it be that the Plupload bind does something to my returned data?

This is my js script:

  uploader.bind('FileUploaded', function(up, file, data) {
    alert(data.toSource());
    $('#' + file.id + " b").html("100%");
  });

 Answers

3

Can anyone tell me why json_encode adds slashes?

Forward slash characters can cause issues (when preceded by a < it triggers the SGML rules for "end of script element") when embedded in an HTML script element. They are escaped as a precaution.

Because when I try do use jQuery.parseJSON(response); in my js script, it returns null. So my guess it has something to do with the slashes.

It doesn't. In JSON "/" and "/" are equivalent.

The JSON you list in the question is valid (you can test it with jsonlint). Your problem is likely to do with what happens to it between json_encode and parseJSON.

Monday, August 22, 2022
5

Try something like this:

//initialize array
$myArray = array();

//set up the nested associative arrays using literal array notation
$firstArray = array("id" => 1, "data" => 45);
$secondArray = array("id" => 3, "data" => 54);

//push items onto main array with bracket notation (this will result in numbered indexes)
$myArray[] = $firstArray;
$myArray[] = $secondArray;

//convert to json
$json = json_encode($myArray);
Friday, December 23, 2022
1

No. The Base64 alphabet includes A-Z, a-z, 0-9 and + and /.

You can replace them if you don't care about portability towards other applications.

See: http://en.wikipedia.org/wiki/Base64#Variants_summary_table

You can use something like these to use your own symbols instead (replace - and _ by anything you want, as long as it is not in the base64 base alphabet, of course!).

The following example converts the normal base64 to base64url as specified in RFC 4648:

function base64url_encode($s) {
    return str_replace(array('+', '/'), array('-', '_'), base64_encode($s));
}

function base64url_decode($s) {
    return base64_decode(str_replace(array('-', '_'), array('+', '/'), $s));
}
Monday, August 1, 2022
 
kirschi
 
4

This appears to be a bug with global TS definitions and "module": "commonjs" in the tsconfig.json.

You can either use global TS definitions and stitch all your output into a single file, or you can use modules and directly import them.

The error here is due to the require returning the module context, and the name of the default being irrelevant - it always becomes default...

declare var thing: ThingStatic;
export thing; // Explicit export for thing
export default thing; // Default export for thing

Now require will return this context, so with commonjs modules:

import module from 'thing';
var thing = module.default; // From the default export
var alsoThing = module.thing; // From the named export

However, I've found this to be inconsistent, so switched to es6 modules:

import thing from './thing'; // Import default
import { thing } from './thing'; // Import named
const thing = (await import('path/to/thing.js')).default; // Import dynamic 
Wednesday, November 23, 2022
 
awiebe
 
4

you can use it like this, in JSON format when you evaluate false value it will give you blank, and when you evaluate true it will give you 1.

$str = '[{"clientId":"17295c59-4373-655a-1141-994aec1779dc","channel":"/meta/connect","connectionType":"long-polling","ext":{"fm.ack":false,"fm.sessionId":"22b0bdcf-4a35-62fc-3764-db4caeece44b"},"id":"5"}]';

$arr = json_decode($str,true);

if($arr[0]['ext']['fm.ack'])    // suggested by **mario**
{
    echo "true";    
}
else {
    echo "false";   
}
Thursday, October 6, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :