我正在开发一个使用PHP生成JSON的应用程序。我的代码在这里。
$arrayItem = array_slice($data, $start, $max);
$jsonArray = array();
$jsonArray['total'] = count($arrayItem);
for ($i = 0; $i <= 9; $i ++) {
// Listomatic requires the "total" field to show/hide the "Show More" button
// The following is sample data (in this case Powerball numbers) that you want to display
$item = array(
'id' => $arrayItem[$i]['id'],
'title' => $arrayItem[$i]['title'],
'featured_image' => $arrayItem[$i]['featured_image'],
'audio_file' => $arrayItem[$i]['audio_file'],
'youtube_id' => $arrayItem[$i]['youtube_id'],
'open_par' => $arrayItem[$i]['open_par'],
'category' => $arrayItem[$i]['category']);
$list_array[] = array('item' =>$item);
}
$jsonArray['items'] = $list_array;
header('Content-type: application/json');
echo json_encode($jsonArray);
它生成以下JSON。我不确定为什么要添加零。
{
"total": 10,
"items": [
{
"item": {
"id": "234",
"title": {
"0": "Indulged in any Good Vices Lately?"
},
"featured_image": {
"0": "http://www.christianquestions.net/wp-content/uploads/2004/05/cq_small_logo-310x227.jpg"
},
"audio_file": {
"0": "http://www.christianquestions.net/en/2002mp3/234_Indulge_in_any_good_vices_lately_12_15_02.mp3"
},
"youtube_id": {},
"open_par": {},
"category": {
"0": "Social Issues"
}
}
},
{
"item": {
"id": "233",
"title": {
"0": "Do You Use the Spirit of a Sound Mind?"
},
"featured_image": {
"0": "http://www.christianquestions.net/wp-content/uploads/2004/05/cq_small_logo-310x227.jpg"
},
"audio_file": {
"0": "http://www.christianquestions.net/en/2002mp3/233_Do_you_use_the_spirit_of_a_sound_mind_12_08_02.mp3"
},
"youtube_id": {},
"open_par": {},
"category": {
"0": "Parable"
}
}
},
{
"item": {
"id": "232",
"title": {
"0": "What Does it Mean to be Thankful?"
},
"featured_image": {
"0": "http://www.christianquestions.net/wp-content/uploads/2004/05/cq_small_logo-310x227.jpg"
},
"audio_file": {
"0": "http://www.christianquestions.net/en/2002mp3/232_What_does_it_mean_to_be_thankful_12_01_02.mp3"
},
"youtube_id": {},
"open_par": {},
"category": {
"0": "Inspiration"
}
}
},
{
"item": {
"id": "226",
"title": {
"0": "Who is Jesus?"
},
"featured_image": {
"0": "http://www.christianquestions.net/wp-content/uploads/2004/05/cq_small_logo-310x227.jpg"
},
"audio_file": {
"0": "http://www.christianquestions.net"
},
"youtube_id": {},
"open_par": {},
"category": {
"0": "Doctrine"
}
}
},
{
"item": {
"id": "216",
"title": {
"0": "Does God Really Torture People in Hellfire?"
},
"featured_image": {
"0": "http://www.christianquestions.net/wp-content/uploads/2004/05/cq_small_logo-310x227.jpg"
},
"audio_file": {
"0": "http://www.christianquestions.net/en/2002mp3/216_Does_God_really_torture_Hellfire.mp3"
},
"youtube_id": {},
"open_par": {},
"category": {
"0": "Doctrine"
}
}
},
{
"item": {
"id": "192",
"title": {
"0": "Why Don’t You Ever Listen to Me?"
},
"featured_image": {
"0": "http://www.christianquestions.net/wp-content/uploads/2004/05/cq_small_logo-310x227.jpg"
},
"audio_file": {
"0": "http://www.christianquestions.net/en/2002mp3/192_Why_dont_you_ever_listen_to_me_02_10_02.mp3"
},
"youtube_id": {},
"open_par": {},
"category": {
"0": "Character"
}
}
},
{
"item": {
"id": "188",
"title": {
"0": "Who Ends Up in the “Lake of Fire?”"
},
"featured_image": {
"0": "http://www.christianquestions.net/wp-content/uploads/2004/05/cq_small_logo-310x227.jpg"
},
"audio_file": {
"0": "http://www.christianquestions.net/en/2002mp3/188_Who_ends_up_in_the_lake_of_fire.mp3"
},
"youtube_id": {},
"open_par": {},
"category": {
"0": "Doctrine"
}
}
},
{
"item": {
"id": "156",
"title": {
"0": "Is Jesus Really God? (Parts III & IV)"
},
"featured_image": {
"0": "http://www.christianquestions.net/wp-content/uploads/2004/05/cq_small_logo-310x227.jpg"
},
"audio_file": {
"0": "http://www.christianquestions.net/en/2001mp3/156_Is_Jesus_really_God.mp3"
},
"youtube_id": {},
"open_par": {},
"category": {
"0": "Doctrine"
}
}
},
{
"item": {
"id": "155",
"title": {
"0": "Is Jesus Really God? (Parts I & II)"
},
"featured_image": {
"0": "http://www.christianquestions.net/wp-content/uploads/2004/05/cq_small_logo-310x227.jpg"
},
"audio_file": {
"0": "http://www.christianquestions.net/en/2001mp3/155_Is_Jesus_really_God.mp3"
},
"youtube_id": {},
"open_par": {},
"category": {
"0": "Doctrine"
}
}
},
{
"item": {
"id": "124",
"title": {
"0": "Did God Really Create the World in 6 Days?"
},
"featured_image": {
"0": "http://www.christianquestions.net/wp-content/uploads/2004/05/cq_small_logo-310x227.jpg"
},
"audio_file": {
"0": "http://www.christianquestions.net/en/2000mp3/124_Did_God_create_the_world_in_6_days_10_22_00.mp3"
},
"youtube_id": {},
"open_par": {},
"category": {
"0": "Doctrine"
}
}
}
]
}
虽然我需要输出以下JSON。
{
"total": 10,
"items": [
{
"item": {
"id": "234",
"title":"Indulged in any Good Vices Lately?",
"featured_image": "http://www.christianquestions.net/wp-content/uploads/2004/05/cq_small_logo-310x227.jpg",
"audio_file": "http://www.christianquestions.net/en/2002mp3/234_Indulge_in_any_good_vices_lately_12_15_02.mp3",
"youtube_id": "http://youtube.com/",
"open_par": "",
"category": "Social Issues"
}
},
{
"item": {
"id": "233",
"title":"Do You Use the Spirit of a Sound Mind?",
"featured_image": "http://www.christianquestions.net/wp-content/uploads/2004/05/cq_small_logo-310x227.jpg",
"audio_file": "http://www.christianquestions.net/en/2002mp3/233_Do_you_use_the_spirit_of_a_sound_mind_12_08_02.mp3",
"youtube_id": "http://youtube.com/",
"open_par": "",
"category": "Parable"
}
},
{
"item": {
"id": "232",
"title": "What Does it Mean to be Thankful?",
"featured_image": "http://www.christianquestions.net/wp-content/uploads/2004/05/cq_small_logo-310x227.jpg",
"audio_file": "http://www.christianquestions.net/en/2002mp3/232_What_does_it_mean_to_be_thankful_12_01_02.mp3",
"youtube_id": "http://youtube.com/v",
"open_par": "",
"category": "Inspiration"
}
}
]
}
忘记空白值我只需要JSON结构。请在我的代码中建议错误。感谢
编辑。我的arrayItem var_dump如下。
array(10) {
[0]=>
array(7) {
["id"]=>
string(3) "234"
["title"]=>
object(SimpleXMLElement)#3306 (1) {
[0]=>
string(34) "Indulged in any Good Vices Lately?"
}
["featured_image"]=>
object(SimpleXMLElement)#3319 (1) {
[0]=>
string(86) "http://www.christianquestions.net/wp-content/uploads/2004/05/cq_small_logo-310x227.jpg"
}
["audio_file"]=>
object(SimpleXMLElement)#3314 (1) {
[0]=>
string(94) "http://www.christianquestions.net/en/2002mp3/234_Indulge_in_any_good_vices_lately_12_15_02.mp3"
}
["youtube_id"]=>
object(SimpleXMLElement)#3315 (0) {
}
["open_par"]=>
object(SimpleXMLElement)#3310 (0) {
}
["category"]=>
object(SimpleXMLElement)#3323 (1) {
[0]=>
string(13) "Social Issues"
}
}
[1]=>
array(7) {
["id"]=>
string(3) "233"
["title"]=>
object(SimpleXMLElement)#3312 (1) {
[0]=>
string(38) "Do You Use the Spirit of a Sound Mind?"
}
["featured_image"]=>
object(SimpleXMLElement)#3325 (1) {
[0]=>
string(86) "http://www.christianquestions.net/wp-content/uploads/2004/05/cq_small_logo-310x227.jpg"
}
["audio_file"]=>
object(SimpleXMLElement)#3320 (1) {
[0]=>
string(99) "http://www.christianquestions.net/en/2002mp3/233_Do_you_use_the_spirit_of_a_sound_mind_12_08_02.mp3"
}
["youtube_id"]=>
object(SimpleXMLElement)#3321 (0) {
}
["open_par"]=>
object(SimpleXMLElement)#3316 (0) {
}
["category"]=>
object(SimpleXMLElement)#3329 (1) {
[0]=>
string(7) "Parable"
}
}
[2]=>
array(7) {
["id"]=>
string(3) "232"
["title"]=>
object(SimpleXMLElement)#3318 (1) {
[0]=>
string(33) "What Does it Mean to be Thankful?"
}
["featured_image"]=>
object(SimpleXMLElement)#3331 (1) {
[0]=>
string(86) "http://www.christianquestions.net/wp-content/uploads/2004/05/cq_small_logo-310x227.jpg"
}
["audio_file"]=>
object(SimpleXMLElement)#3326 (1) {
[0]=>
string(94) "http://www.christianquestions.net/en/2002mp3/232_What_does_it_mean_to_be_thankful_12_01_02.mp3"
}
["youtube_id"]=>
object(SimpleXMLElement)#3327 (0) {
}
["open_par"]=>
object(SimpleXMLElement)#3322 (0) {
}
["category"]=>
object(SimpleXMLElement)#3335 (1) {
[0]=>
string(11) "Inspiration"
}
}
[3]=>
array(7) {
["id"]=>
string(3) "226"
["title"]=>
object(SimpleXMLElement)#3324 (1) {
[0]=>
string(13) "Who is Jesus?"
}
["featured_image"]=>
object(SimpleXMLElement)#3337 (1) {
[0]=>
string(86) "http://www.christianquestions.net/wp-content/uploads/2004/05/cq_small_logo-310x227.jpg"
}
["audio_file"]=>
object(SimpleXMLElement)#3332 (1) {
[0]=>
string(33) "http://www.christianquestions.net"
}
["youtube_id"]=>
object(SimpleXMLElement)#3333 (0) {
}
["open_par"]=>
object(SimpleXMLElement)#3328 (0) {
}
["category"]=>
object(SimpleXMLElement)#3341 (1) {
[0]=>
string(8) "Doctrine"
}
}
[4]=>
array(7) {
["id"]=>
string(3) "216"
["title"]=>
object(SimpleXMLElement)#3330 (1) {
[0]=>
string(43) "Does God Really Torture People in Hellfire?"
}
["featured_image"]=>
object(SimpleXMLElement)#3343 (1) {
[0]=>
string(86) "http://www.christianquestions.net/wp-content/uploads/2004/05/cq_small_logo-310x227.jpg"
}
["audio_file"]=>
object(SimpleXMLElement)#3338 (1) {
[0]=>
string(85) "http://www.christianquestions.net/en/2002mp3/216_Does_God_really_torture_Hellfire.mp3"
}
["youtube_id"]=>
object(SimpleXMLElement)#3339 (0) {
}
["open_par"]=>
object(SimpleXMLElement)#3334 (0) {
}
["category"]=>
object(SimpleXMLElement)#3347 (1) {
[0]=>
string(8) "Doctrine"
}
}
[5]=>
array(7) {
["id"]=>
string(3) "192"
["title"]=>
object(SimpleXMLElement)#3336 (1) {
[0]=>
string(34) "Why Don’t You Ever Listen to Me?"
}
["featured_image"]=>
object(SimpleXMLElement)#3349 (1) {
[0]=>
string(86) "http://www.christianquestions.net/wp-content/uploads/2004/05/cq_small_logo-310x227.jpg"
}
["audio_file"]=>
object(SimpleXMLElement)#3344 (1) {
[0]=>
string(92) "http://www.christianquestions.net/en/2002mp3/192_Why_dont_you_ever_listen_to_me_02_10_02.mp3"
}
["youtube_id"]=>
object(SimpleXMLElement)#3345 (0) {
}
["open_par"]=>
object(SimpleXMLElement)#3340 (0) {
}
["category"]=>
object(SimpleXMLElement)#3353 (1) {
[0]=>
string(9) "Character"
}
}
[6]=>
array(7) {
["id"]=>
string(3) "188"
["title"]=>
object(SimpleXMLElement)#3342 (1) {
[0]=>
string(38) "Who Ends Up in the “Lake of Fire?”"
}
["featured_image"]=>
object(SimpleXMLElement)#3355 (1) {
[0]=>
string(86) "http://www.christianquestions.net/wp-content/uploads/2004/05/cq_small_logo-310x227.jpg"
}
["audio_file"]=>
object(SimpleXMLElement)#3350 (1) {
[0]=>
string(84) "http://www.christianquestions.net/en/2002mp3/188_Who_ends_up_in_the_lake_of_fire.mp3"
}
["youtube_id"]=>
object(SimpleXMLElement)#3351 (0) {
}
["open_par"]=>
object(SimpleXMLElement)#3346 (0) {
}
["category"]=>
object(SimpleXMLElement)#3359 (1) {
[0]=>
string(8) "Doctrine"
}
}
[7]=>
array(7) {
["id"]=>
string(3) "156"
["title"]=>
object(SimpleXMLElement)#3348 (1) {
[0]=>
string(37) "Is Jesus Really God? (Parts III & IV)"
}
["featured_image"]=>
object(SimpleXMLElement)#3361 (1) {
[0]=>
string(86) "http://www.christianquestions.net/wp-content/uploads/2004/05/cq_small_logo-310x227.jpg"
}
["audio_file"]=>
object(SimpleXMLElement)#3356 (1) {
[0]=>
string(72) "http://www.christianquestions.net/en/2001mp3/156_Is_Jesus_really_God.mp3"
}
["youtube_id"]=>
object(SimpleXMLElement)#3357 (0) {
}
["open_par"]=>
object(SimpleXMLElement)#3352 (0) {
}
["category"]=>
object(SimpleXMLElement)#3365 (1) {
[0]=>
string(8) "Doctrine"
}
}
[8]=>
array(7) {
["id"]=>
string(3) "155"
["title"]=>
object(SimpleXMLElement)#3354 (1) {
[0]=>
string(35) "Is Jesus Really God? (Parts I & II)"
}
["featured_image"]=>
object(SimpleXMLElement)#3367 (1) {
[0]=>
string(86) "http://www.christianquestions.net/wp-content/uploads/2004/05/cq_small_logo-310x227.jpg"
}
["audio_file"]=>
object(SimpleXMLElement)#3362 (1) {
[0]=>
string(72) "http://www.christianquestions.net/en/2001mp3/155_Is_Jesus_really_God.mp3"
}
["youtube_id"]=>
object(SimpleXMLElement)#3363 (0) {
}
["open_par"]=>
object(SimpleXMLElement)#3358 (0) {
}
["category"]=>
object(SimpleXMLElement)#3371 (1) {
[0]=>
string(8) "Doctrine"
}
}
[9]=>
array(7) {
["id"]=>
string(3) "124"
["title"]=>
object(SimpleXMLElement)#3360 (1) {
[0]=>
string(42) "Did God Really Create the World in 6 Days?"
}
["featured_image"]=>
object(SimpleXMLElement)#3373 (1) {
[0]=>
string(86) "http://www.christianquestions.net/wp-content/uploads/2004/05/cq_small_logo-310x227.jpg"
}
["audio_file"]=>
object(SimpleXMLElement)#3368 (1) {
[0]=>
string(96) "http://www.christianquestions.net/en/2000mp3/124_Did_God_create_the_world_in_6_days_10_22_00.mp3"
}
["youtube_id"]=>
object(SimpleXMLElement)#3369 (0) {
}
["open_par"]=>
object(SimpleXMLElement)#3364 (0) {
}
["category"]=>
object(SimpleXMLElement)#3377 (1) {
[0]=>
string(8) "Doctrine"
}
}
}
答案 0 :(得分:1)
如var_dump中所示,标题不是字符串,而是对象,导致此行为。如果你希望json_encode将值编码为字符串而不是对象,请确保给它一个字符串,例如通过转换值:
'title' => (string)($arrayItem[$i]['title']),
编辑:请注意,字符串转换并不总是那么简单。在这种情况下,只需要进行转换,因为我们正在处理SimpleXMLElement对象。如果您有常规对象/数组,则可能需要使用更合适的内容(例如$myarray[0]
)。