在PHP中的JSONObject内创建JSONArray

时间:2019-06-18 17:53:45

标签: php json

我有以下代码...

 $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。

$ resp ...

<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>
<oneDayShippingAvailable>false</oneDayShippingAvailable>
<handlingTime>3</handlingTime>
</shippingInfo>
<sellingStatus>
<currentPrice currencyId="USD">104.99</currentPrice>
<convertedCurrentPrice currencyId="USD">104.99</convertedCurrentPrice>
<sellingState>EndedWithSales</sellingState>
</sellingStatus>
<listingInfo>
<bestOfferEnabled>false</bestOfferEnabled>
<buyItNowAvailable>false</buyItNowAvailable>
<startTime>2019-06-18T00:18:54.000Z</startTime>
<endTime>2019-06-18T17:29:04.000Z</endTime>
<listingType>FixedPrice</listingType>
<gift>false</gift>
<watchCount>3</watchCount>
</listingInfo>
<returnsAccepted>false</returnsAccepted>
<condition>
<conditionId>1000</conditionId>
<conditionDisplayName>Brand New</conditionDisplayName>
</condition>
<isMultiVariationListing>false</isMultiVariationListing>
<topRatedListing>false</topRatedListing>
</item>
</searchResult>
<paginationOutput>
<pageNumber>1</pageNumber>
<entriesPerPage>1</entriesPerPage>
<totalPages>343</totalPages>
<totalEntries>343</totalEntries>
</paginationOutput>
</findCompletedItemsResponse>

我在做什么错了?

1 个答案:

答案 0 :(得分:1)

使用var_dump()检查值将有助于解决此类问题。

问题在于$itemId是一个SimpleXmlElement,并且没有按照您期望的方式进行编码。要针对特定​​情况解决此问题(可能会随不同或更多可变数据而变化),则可以根据需要强制转换该值。

$itemId = strval($item->itemId);

https://3v4l.org/j7meB