Jump to content

json encode


ohdang888

Recommended Posts

This array:

Array
(
    [error] => 
    [errorMessage] => 
    [numberOfSongs] => 6
    [music] => Array
        (
            [0] => Array
                (
                    [title] => Someday
                    [artist] => Rob Thomas
                    [album] => Unknown
                    [time] => 
                    [plays] => 1
                    [songID] => 2
                    [ytID] => CVWvCJcse8w
                )

            [1] => Array
                (
                    [title] => Testing title 1st
                    [artist] => artist of 1st
                    [album] => 
                    [time] => 
                    [plays] => 0
                    [songID] => 1
                    [ytID] => 
                )

        )

)

with

json_encode($my_array);

is turning it into this:

{"error":false,"errorMessage":null,"numberOfSongs":6,"music":[{"title":"Someday","artist":"Rob Thomas","album":"Unknown","time":"","plays":"1","songID":"2","ytID":"CVWvCJcse8w"},{"title":"Testing title 1st","artist":"artist of 1st","album":"","time":"","plays":"0","songID":"1","ytID":""}]};

Notice how there is no "0" or "1" array, it just simply puts it in there without something as the key...so i want this:

{"error":false,"errorMessage":null,"numberOfSongs":6,"music":["0":{"title":"Someday","artist":"Rob Thomas","album":"Unknown","time":"","plays":"1","songID":"2","ytID":"CVWvCJcse8w"},"1":{"title":"Testing title 1st","artist":"artist of 1st","album":"","time":"","plays":"0","songID":"1","ytID":""}]};

 

How is that done??

 

Thanks.

 

 

Link to comment
https://forums.phpfreaks.com/topic/184561-json-encode/
Share on other sites

Well i'm assuming the issue is the JSON i created

 

 

here is my old, handmade JSON, when i use it, my site works.

playerData.music_lib = { error:"false", errorMessage: null, numberOfSongs:6,
music:{ 
1:{title: "Someday", artist:"Rob Thomas", album:"Unknown", time:"1:45", plays:4, songID:"234526", ytID:"CVWvCJcse8w"},
2:{title: "Look Back At Me", artist:"Trina", album:"Unknown", time:"1:15", plays:1, songID:"153456", ytID:"xszqrOffFuw"}
}
};

 

 

when i use this (from json_encode):

playerData.music_lib = {"error":false,"errorMessage":null,"numberOfSongs":6,"music":[{"title":"Someday","artist":"Rob Thomas","album":"Unknown","time":"","plays":"1","songID":"2","ytID":"CVWvCJcse8w"},{"title":"Testing title 1st","artist":"artist of 1st","album":"","time":"","plays":"0","songID":"1","ytID":""}]};

it doesn't work..

 

This is my JS function (which works with the handmade JSON)

function loadBaseSongs(data){
   if(data.error == "false"){
     jQuery.each(data.music, function(key, song) {
        //FORMAT: title, artist, time, album, plays
       $("#current_playlist").append("<tr><td><a href='#' onClick='playNewSong(\"" + song.ytID + "\"," + key + ");'>" + song.title + "</a></td><td>" +    song.artist + "</td><td>" + song.time + "</td><td>" + song.album + "</td><td id='" + key +"_playCount'>" + song.plays + "</td></tr>");
       });//end each

    }
}
[code]

Link to comment
https://forums.phpfreaks.com/topic/184561-json-encode/#findComment-974367
Share on other sites

any other solutions? any way i could modify my javascript possibly??

 

In your JavaScript, use if(!data.error){ or if(data.error == false){ in place of if(data.error == "false"){

 

will do, thanks. I will, but there are larger issues at hand for me. This puts the songs on the html list, but my other function rely on that "key"...the "0" or "1" in front of it.

Link to comment
https://forums.phpfreaks.com/topic/184561-json-encode/#findComment-975034
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.