如何解码php中的json以插入数据库

时间:2016-07-23 06:36:30

标签: php json

我的json数据如下: -

{" _id" {" $ OID":" 56f7691531cd90e6117b375b"}"使用者" {" $ OID& #34;:" 56f7691131cd90e6117b375a"}," __ v":0} {" _id":{" $ oid":" 56f8954771af9a655f73aa31"}," user":{" $ oid":" 56f8954671af9a655f73aa30"}," __ v":0} {&# 34; _id" {" $ OID":" 56fac95cc0ba33890825e89b"}"使用者" {" $ OID":&# 34; 56fac95bc0ba33890825e89a"}," __ v":0} {" _id":{" $ oid":" 56facb44c0ba33890825e8a0"}, "用户":{" $ oid":" 56facb44c0ba33890825e89f"}," __ v":0} {" _id" :{" $ OID":" 56fad24553620b8f0cd4e520"}"使用者" {" $ OID":" 56fad24553620b8f0cd4e51f" }," __ v":0} {" _id":{" $ oid":" 56fc161cbd67182d0fa84131"}," user&# 34;:{" $ oid":" 56fc161cbd67182d0fa84130"}," __ v":0} {" _id":{" $ OID":" 56fe839fbd67182d0fa84153"}"用户" {" $ OI d":" 56fe839fbd67182d0fa84152"}," __ v":0} {" _id":{" $ oid":&#34 ; 570baf411560f443514d247e"}," user":{" $ oid":" 570baf401560f443514d247d"}," __ v":0} {& #34; _id" {" $ OID":" 571a30c93a11a56244a5d2aa"}"使用者" {" $ OID":& #34; 571a30c93a11a56244a5d2a9"}," __ v":0} {" _id":{" $ oid":" 571a36183a11a56244a5d2b7"} ,"用户":{" $ oid":" 571a36183a11a56244a5d2b6"}," __ v":0} {" _id&#34 ;:{" $ OID":" 571a39393a11a56244a5d2b9"}"使用者" {" $ OID":" 571a39393a11a56244a5d2b8&#34 ;}," __ v":0} {" _id":{" $ oid":" 571a45cf3a11a56244a5d2bb"},"用户& #34;:{" $ oid":" 571a45cf3a11a56244a5d2ba"}," __ v":0} {" _id":{&#34 ; $ OID":" 571a499a3a11a56244a5d2c9"}"使用者" {" $ OID":" 571a499a3a11a56244a5d2c8"}&#34 ; __ v":0} {" _id":{" $ oid":&#3 4; 571a64f60f97b4d16c188430"},"用户":{" $ oid":" 571a64f60f97b4d16c18842f"}," __ v":0} { " _id" {" $ OID":" 5722ecb29787b9a651b727dd"}"使用者" {" $ OID&#34 ;: " 5722ecb19787b9a651b727dc"}," __ v":0} {" _id":{" $ oid":" 57265da014d49e6a6e4c8e20" },"用户":{" $ oid":" 57265da014d49e6a6e4c8e1f"}," __ v":0} {" _id&# 34;:{" $ OID":" 572755eb14d49e6a6e4c8e3b"}"使用者" {" $ OID":" 572755eb14d49e6a6e4c8e3a&# 34;}," __ v":0} {" _id":{" $ oid":" 57276d7d14d49e6a6e4c8e5a"},"用户":{" $ oid":" 57276d7c14d49e6a6e4c8e59"}," __ v":0} {" _id":{&# 34; $ OID":" 572cf992ecbaa8224a72a0ce"}"使用者" {" $ OID":" 572cf992ecbaa8224a72a0cd"},&# 34; __ v":0} {" _id":{" $ oid":" 573a14c81e46221540ff8479"}," user":{ " $ OID":"&573a14c81e46221540ff8478 #34;}," __ v":0} {" _id":{" $ oid":" 573f0c7c7f91fcb3274af867"},&#34 ;用户":{" $ oid":" 573f0c7c7f91fcb3274af866"}," __ v":0} {" _id":{& #34; $ OID":" 577760853858cf573d2928f7"}"使用者" {" $ OID":" 577760853858cf573d2928f6"},& #34; __ v":0}

2 个答案:

答案 0 :(得分:0)

我假设每行代码都是返回的单独的json代码行。如果是这样,你可以像这样使用json_decode:

<?php
    $json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';

    var_dump(json_decode($json)); //returns an object
    var_dump(json_decode($json, true));  //returns an array

?>

答案 1 :(得分:0)

您的JSON数据有点奇怪,括号后没有足够的逗号。如果您将尝试此示例进行解码,那么它将起作用:

$json = '[
    {"_id":{"$oid":"56f7691531cd90e6117b375b"},"user":{"$oid":"56f7691131cd90e6117b375a"},"__v":0},
    {"_id":{"$oid":"56f8954771af9a655f73aa31"},"user":{"$oid":"56f8954671af9a655f73aa30"},"__v":0},
    {"_id":{"$oid":"56fac95cc0ba33890825e89b"},"user":{"$oid":"56fac95bc0ba33890825e89a"},"__v":0},
    {"_id":{"$oid":"56facb44c0ba33890825e8a0"},"user":{"$oid":"56facb44c0ba33890825e89f"},"__v":0} 
]';

var_dump(json_decode($json));