Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

quick question i really need some help, i re-formatting some json to get it into a javascript var, i got it working with json_encode but i need it to output ' symbol instead of the " symbol, is there a way to do that

i need it to be like this (using the ' symbol)

{
title:'Greeting',
mp3:'http://www.godsgypsychristianchurch.net/music/Atlanta%20GA/Dey%20duma%20amensa/01%20Greeting.mp3',
buy:'http://www.godsgypsychristianchurch.net/music/Atlanta%20GA/Dey%20duma%20amensa/01%20Greeting.mp3',
price:'Download',
duration:'',
cover:'http://godsgypsychristianchurch.net/music_artwork/DEFAULT_COVER.png'},

My Code:

foreach ($json['rows'] as $row) {
    if ($_GET['churchname'] == $row[doc]['album']) {
        $songCount = 0;
        foreach ($row['doc']['tracks'] as $song) {
            ++$songCount;
            $arr = array(
                "title" => "{$song['name']}",
                "mp3" => "{$songUrl}",
                "buy" => "{$songUrl}",
                "price" => "Download",
                "duration" => "",
                "cover" => "{$row['doc']['artwork']}",
                );
            echo json_encode($arr);
        }
    }
}
exit;
share|improve this question
Can't you just search the result of json_encode() for double quotes and replace them with single quotes? – Lorax Nov 13 '12 at 20:09

1 Answer

1) You can't get it to use the ' ', as the specification for valid-JSON requires " ", so any program expecting to parse your string from JSON to an object in JS/PHP/etc, is going to error out on you

2) Why? JS doesn't care which one you use, one way or another, and if you're doing something on the server-side, leave it as a multi-dimensional (potentially associative) array.

3) If it's for the purpose of including " " inside of your actual string, then escape them with \, like so:

$myString = "This is my \"double-quoted\" string";

$myString === 'This is my "double-quoted" string';
$myString === "This is my " . '"double-quoted"' . "string";

If you are concatenating strings together, and one of those strings already contains double-quotes within the string itself, then your language will automatically ensure they're escaped.

I'd be happy to help further, but I'd need to know the "Why?" part.

share|improve this answer
Thanks for that help, I did not know that I'll give it a shot when I get home, but just to give you a bit more info, here is what I am trying to accomplish but this version is hard coded (static) I'm trying to pull the data from my JSON ggcc.tv/NewSite/cities/atlanta/AllThingsArePossible.html you can view the page source and check it out, sorry I can paste the code in a comment and not to mention from my iPhone - thanks for the help! – Michael Nov 13 '12 at 21:58
Okay, so first a very quick semantic thing: your myPlaylist variable isn't JSON - it's a JS array which holds JS objects. Normally, that sounds rather pedantic, I get that... ...but here's why it's important: 1) JS accepts both ' and ", while JSON accepts only " 2) JSON is a string: "[\"one\",\"two\"]" and is not an object or an array or anything else -- it's just a string that looks like one. The really, really easy solution here, seeing as you've already got myPlaylist written out, is to do something like var json_playlist = JSON.stringify(myPlaylist); then copy the string. – Norguard Nov 13 '12 at 22:41
myPlaylist is perfectly-valid JS, so turning it into a JSON string is a snap. Then you just need to copy that string and that will become your JSON, which you can then JSON.parse(json); back into an object in JS, or json_decode($json, true); it into an associative array in PHP, or whatever languages it will be that you're going to use. – Norguard Nov 13 '12 at 22:50

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.