ohdang888 Posted December 9, 2009 Share Posted December 9, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/184561-json-encode/ Share on other sites More sharing options...
Philip Posted December 9, 2009 Share Posted December 9, 2009 If it's a numerical key, JS will see it as [0], [1],...[n] Quote Link to comment https://forums.phpfreaks.com/topic/184561-json-encode/#findComment-974363 Share on other sites More sharing options...
ohdang888 Posted December 9, 2009 Author Share Posted December 9, 2009 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] Quote Link to comment https://forums.phpfreaks.com/topic/184561-json-encode/#findComment-974367 Share on other sites More sharing options...
Philip Posted December 9, 2009 Share Posted December 9, 2009 Ah, okay, well do you have a PHP version greater than 5.3.0? If so, you can use the JSON_FORCE_OBJECT option Quote Link to comment https://forums.phpfreaks.com/topic/184561-json-encode/#findComment-974370 Share on other sites More sharing options...
Mchl Posted December 9, 2009 Share Posted December 9, 2009 Anyhow, the JSON string you've posted above is invalid according to http://www.jsonlint.com/ Quote Link to comment https://forums.phpfreaks.com/topic/184561-json-encode/#findComment-974375 Share on other sites More sharing options...
Philip Posted December 9, 2009 Share Posted December 9, 2009 Anyhow, the JSON string you've posted above is invalid according to http://www.jsonlint.com/ The only reason is because of the array chars (square brackets [ ] ) vs object chars (curly brackets { } ) Quote Link to comment https://forums.phpfreaks.com/topic/184561-json-encode/#findComment-974377 Share on other sites More sharing options...
ohdang888 Posted December 10, 2009 Author Share Posted December 10, 2009 Ah, okay, well do you have a PHP version greater than 5.3.0? If so, you can use the JSON_FORCE_OBJECT option AHHHHHHHHHHH no i have 5.2.11 any other solutions? any way i could modify my javascript possibly?? Quote Link to comment https://forums.phpfreaks.com/topic/184561-json-encode/#findComment-975025 Share on other sites More sharing options...
salathe Posted December 10, 2009 Share Posted December 10, 2009 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"){ Quote Link to comment https://forums.phpfreaks.com/topic/184561-json-encode/#findComment-975033 Share on other sites More sharing options...
ohdang888 Posted December 10, 2009 Author Share Posted December 10, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/184561-json-encode/#findComment-975034 Share on other sites More sharing options...
salathe Posted December 10, 2009 Share Posted December 10, 2009 That's probably not such a big issue, depending on precisely what you're doing. The items can still be accessed via their array offset (the "key"). Quote Link to comment https://forums.phpfreaks.com/topic/184561-json-encode/#findComment-975040 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.