使用大型.JSON文件(PHP)

时间:2014-01-11 05:12:58

标签: php arrays database json decode

我有一个很大的JSON文件,我试图插入到数据库中。使用json_decode将其转换为数组以将其插入数据库时​​,我收到错误:

Fatal error:  Allowed memory size of 134217728 bytes exhausted (tried to allocate 188340087 bytes) in site.php on line 48

是否有更有效的方式来翻译这些数据而无需逐行读取和缓冲。如果没有,我应该如何阅读每一行并解码它?我很感激任何帮助。

{"items":[ {
  "itemId" : 17730521,
  "parentItemId" : 17730521,
  "name" : "1000 lb Flat-Free Super Steel P-Handle Hand Truck",
  "msrp" : 110.0,
  "salePrice" : 87.94,
  "upc" : "053417031815",
  "categoryPath" : "Home Improvement/Tools/Lifting Equipment",
  "shortDescription" : "The Flat-Free 1000-pound Heavy-Duty Hand Truck is one tough truck with its heavy-duty oversized frame, extra large base plate and curved frame back that easily transports boxes or cylinders.",
  "longDescription" : "<br><b>1000 lb Flat-Free Super Steel P-Handle Hand Truck:</b><ul><li>Heavy-duty flat-free 10" solid rubber wheels</li><li>1000 lb capacity</li><li>Extra large 20" x 8" base plate for bulky and oversized loads</li><li>50" height for larger loads</li><li>P-loop handle for easy 1- or 2-hand control</li><li>Large fenders to protect load from damage</li><li>Dimensions: 50"L x 21"W x 19"H</li><li>1-year warranty</li></ul>",
  "brandName" : "Generic",
  "thumbnailImage" : "this is a link",
  "mediumImage" : "this is a link",
  "largeImage" : "this is a link",
  "productTrackingUrl" : "",
  "ninetySevenCentShipping" : false,
  "standardShipRate" : 0.0,
  "twoThreeDayShippingRate" : 79.97,
  "overnightShippingRate" : 73.97,
  "marketplace" : false,
  "shipToStore" : true,
  "freeShipToStore" : true,
  "modelNumber" : "H5986",
  "productUrl" : "",
  "categoryNode" : "1072864_1031899_1043985",
  "bundle" : false,
  "clearance" : true,
  "preOrder" : false,
  "stock" : "Available",
  "availableOnline" : true,
  "bestMarketplacePrice" : {
    "price" : 120.0,
    "sellerInfo" : "PlumStruck",
    "standardShipRate" : 5.99,
    "availableOnline" : true,
    "clearance" : false
  }
}, 
...
]}

1 个答案:

答案 0 :(得分:0)

有一个好人写了这个lib来解析大型JSON https://github.com/kuma-giyomu/JSONParser

正如作者所说:

如何:

阅读test / test.php文件以获取真实示例。但基本上是:

创建解析器实例

为您感兴趣的事件设置处理程序

运行parseDocument方法

等一下