Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.