猿问

在 PHP 中的 JSONObject 内部创建 JSONArray

我有以下代码...


 $mainResponse = array(

                 "result" => array(

                 ),

                 "ack" => "success"

            );


       foreach($resp->searchResult->item as $item) {

         $itemId = $item->itemId;


         $addThisItem = array("itemId" => $itemId);

         array_push($mainResponse['result'], $addThisItem);

     }

}


   echo json_encode($mainResponse);

输出:


{"result":[{"itemId":{"0":"183851233368"}}],"ack":"success"}

预期产出:


{"result":[{"itemId":"183851233368"},...],"ack":"success"}

编辑:


响应作为 XML 接收。我用$resp = simplexml_load_file($apicall);


$resp->searchResult->item

这是一个项目列表。像......[{"itemId":"1"},{"itemId":"2"}...]包含我需要的一切。如果我打印$itemId,我会得到我的 itemId。


$分别...


<findCompletedItemsResponse xmlns="">

<ack>Success</ack>

<version>1.13.0</version>

<timestamp>2019-06-18T18:23:20.844Z</timestamp>

<searchResult count="1">

<item>

<itemId>183851233368</itemId>

<title>

2019 Bowman Mega Box SEALED LOT 4 Wander Franco Vlad Joey Bart TARGET Exclusive!

</title>

<globalId>EBAY-US</globalId>

<primaryCategory>

<categoryId>213</categoryId>

<categoryName>Baseball Cards</categoryName>

</primaryCategory>

<galleryURL>

http://thumbs1.ebaystatic.com/m/msMvr_Xz-51zlz4it4tOOLw/140.jpg

</galleryURL>

<viewItemURL>

http://www.ebay.com/itm/2019-Bowman-Mega-Box-SEALED-LOT-4-Wander-Franco-Vlad-Joey-Bart-TARGET-Exclusive-/183851233368

</viewItemURL>

<paymentMethod>PayPal</paymentMethod>

<autoPay>false</autoPay>

<postalCode>60098</postalCode>

<location>Woodstock,IL,USA</location>

<country>US</country>

<shippingInfo>

<shippingServiceCost currencyId="USD">4.99</shippingServiceCost>

<shippingType>Flat</shippingType>

<shipToLocations>Worldwide</shipToLocations>

<expeditedShipping>true</expeditedShipping>

我究竟做错了什么?


神不在的星期二
浏览 335回答 1
1回答
随时随地看视频慕课网APP
我要回答