我正在寻找一种通过REST api从mongodb json导出更新数据库的方法。
基本上,我想要了解" _id"在这个jsonfile中的对象,我希望在Save()
我已尝试过多种循环使用php的方法,但似乎无法掌握处理对象的正确方法。
JSON导出示例
POST 'someurl/thisapi'
尝试了以下方法,但没有成功
$json = '{ "_id" : { "$oid" : "5922005ccffbfad13f1f95df" }, "Name" : "pending", "Position" : "pending", "Company" : "pending", "Description" : "pending", "Country" : "pending", "Zip" : "pending", "City" : "pending", "State" : "pending", "Address" : "pending", "Email" : "pending", "Website" : "pending", "Phonenumber" : "pending", "CompanyNumber" : 8209948, "CompanyName" : "! LTD", "RegAddressAddressLine1" : "METROHOUSE 57 PEPPER ROAD", "RegAddressAddressLine2" : "HUNSLET", "RegAddressPostTown" : "LEEDS", "RegAddressCounty" : "YORKSHIRE", "RegAddressCountry" : "", "RegAddressPostCode" : "LS10 2RU", "CompanyCategory" : "Private Limited Company", "CompanyStatus" : "Active", "CountryOfOrigin" : "United Kingdom", "DissolutionDate" : "", "IncorporationDate" : "11/09/2012", "SICCodeSicText_1" : "99999 - Dormant Company", "URI" : "http://business.data.gov.uk/id/company/08209948" }
{ "_id" : { "$oid" : "5922005ccffbfad13f1f95e0" }, "Name" : "pending", "Position" : "pending", "Company" : "pending", "Description" : "pending", "Country" : "pending", "Zip" : "pending", "City" : "pending", "State" : "pending", "Address" : "pending", "Email" : "pending", "Website" : "pending", "Phonenumber" : "pending", "CompanyNumber" : 7382019, "CompanyName" : "!BIG IMPACT GRAPHICS LIMITED", "RegAddressAddressLine1" : "335 ROSDEN HOUSE", "RegAddressAddressLine2" : "372 OLD STREET", "RegAddressPostTown" : "LONDON", "RegAddressCounty" : "", "RegAddressCountry" : "", "RegAddressPostCode" : "EC1V 9AV", "CompanyCategory" : "Private Limited Company", "CompanyStatus" : "Active", "CountryOfOrigin" : "United Kingdom", "DissolutionDate" : "", "IncorporationDate" : "21/09/2010", "SICCodeSicText_1" : "59112 - Video production activities", "URI" : "http://business.data.gov.uk/id/company/07382019" }
{ "_id" : { "$oid" : "5922005ccffbfad13f1f95e1" }, "Name" : "pending", "Position" : "pending", "Company" : "pending", "Description" : "pending", "Country" : "pending", "Zip" : "pending", "City" : "pending", "State" : "pending", "Address" : "pending", "Email" : "pending", "Website" : "pending", "Phonenumber" : "pending", "CompanyNumber" : 4753368, "CompanyName" : "!NFERNO LTD.", "RegAddressAddressLine1" : "FIRST FLOOR THAVIES INN HOUSE 3-4", "RegAddressAddressLine2" : "HOLBORN CIRCUS", "RegAddressPostTown" : "LONDON", "RegAddressCounty" : "", "RegAddressCountry" : "", "RegAddressPostCode" : "EC1N 2HA", "CompanyCategory" : "Private Limited Company", "CompanyStatus" : "Active - Proposal to Strike off", "CountryOfOrigin" : "United Kingdom", "DissolutionDate" : "", "IncorporationDate" : "03/05/2003", "SICCodeSicText_1" : "82990 - Other business support service activities n.e.c.", "URI" : "http://business.data.gov.uk/id/company/04753368" }
{ "_id" : { "$oid" : "5922005ccffbfad13f1f95e2" }, "Name" : "pending", "Position" : "pending", "Company" : "pending", "Description" : "pending", "Country" : "pending", "Zip" : "pending", "City" : "pending", "State" : "pending", "Address" : "pending", "Email" : "pending", "Website" : "pending", "Phonenumber" : "pending", "CompanyNumber" : "SC421617", "CompanyName" : "!NSPIRED LTD", "RegAddressAddressLine1" : "26 POLMUIR ROAD", "RegAddressAddressLine2" : "", "RegAddressPostTown" : "ABERDEEN", "RegAddressCounty" : "", "RegAddressCountry" : "UNITED KINGDOM", "RegAddressPostCode" : "AB11 7SY", "CompanyCategory" : "Private Limited Company", "CompanyStatus" : "Active", "CountryOfOrigin" : "United Kingdom", "DissolutionDate" : "", "IncorporationDate" : "11/04/2012", "SICCodeSicText_1" : "70229 - Management consultancy activities other than financial management", "URI" : "http://business.data.gov.uk/id/company/SC421617" }
{ "_id" : { "$oid" : "5922005ccffbfad13f1f95e3" }, "Name" : "pending", "Position" : "pending", "Company" : "pending", "Description" : "pending", "Country" : "pending", "Zip" : "pending", "City" : "pending", "State" : "pending", "Address" : "pending", "Email" : "pending", "Website" : "pending", "Phonenumber" : "pending", "CompanyNumber" : 9152972, "CompanyName" : "!NVERTD DESIGNS LIMITED", "RegAddressAddressLine1" : "32 RECTORY ROAD", "RegAddressAddressLine2" : "", "RegAddressPostTown" : "STEPPINGLEY", "RegAddressCounty" : "", "RegAddressCountry" : "", "RegAddressPostCode" : "MK45 5AT", "CompanyCategory" : "Private Limited Company", "CompanyStatus" : "Active", "CountryOfOrigin" : "United Kingdom", "DissolutionDate" : "", "IncorporationDate" : "30/07/2014", "SICCodeSicText_1" : "58190 - Other publishing activities", "URI" : "http://business.data.gov.uk/id/company/09152972" }
{ "_id" : { "$oid" : "5922005ccffbfad13f1f95e4" }, "Name" : "pending", "Position" : "pending", "Company" : "pending", "Description" : "pending", "Country" : "pending", "Zip" : "pending", "City" : "pending", "State" : "pending", "Address" : "pending", "Email" : "pending", "Website" : "pending", "Phonenumber" : "pending", "CompanyNumber" : "FC031362", "CompanyName" : "!OBAC LIMITED", "RegAddressAddressLine1" : "1ST AND 2ND FLOORS ELIZABETH HOUSE", "RegAddressAddressLine2" : "LES RUETIES BRAYES", "RegAddressPostTown" : "ST PETER PORT", "RegAddressCounty" : "GUERNSEY", "RegAddressCountry" : "GUERNSEY", "RegAddressPostCode" : "GY1 1EW", "CompanyCategory" : "Other company type", "CompanyStatus" : "Active", "CountryOfOrigin" : "CHANNEL ISLANDS", "DissolutionDate" : "", "IncorporationDate" : "30/11/2012", "SICCodeSicText_1" : "None Supplied", "URI" : "http://business.data.gov.uk/id/company/FC031362" }
{ "_id" : { "$oid" : "5922005ccffbfad13f1f95e5" }, "Name" : "pending", "Position" : "pending", "Company" : "pending", "Description" : "pending", "Country" : "pending", "Zip" : "pending", "City" : "pending", "State" : "pending", "Address" : "pending", "Email" : "pending", "Website" : "pending", "Phonenumber" : "pending", "CompanyNumber" : 7687209, "CompanyName" : "!OBAC UK LIMITED", "RegAddressAddressLine1" : "ENDEAVOUR HOUSE", "RegAddressAddressLine2" : "COOPERS END ROAD", "RegAddressPostTown" : "STANSTED AIRPORT", "RegAddressCounty" : "", "RegAddressCountry" : "", "RegAddressPostCode" : "CM24 1SJ", "CompanyCategory" : "Private Limited Company", "CompanyStatus" : "Active", "CountryOfOrigin" : "United Kingdom", "DissolutionDate" : "", "IncorporationDate" : "29/06/2011", "SICCodeSicText_1" : "70229 - Management consultancy activities other than financial management", "URI" : "http://business.data.gov.uk/id/company/07687209" }
{ "_id" : { "$oid" : "5922005ccffbfad13f1f95e6" }, "Name" : "pending", "Position" : "pending", "Company" : "pending", "Description" : "pending", "Country" : "pending", "Zip" : "pending", "City" : "pending", "State" : "pending", "Address" : "pending", "Email" : "pending", "Website" : "pending", "Phonenumber" : "pending", "CompanyNumber" : 2714021, "CompanyName" : "!YOZO FASS LIMITED", "RegAddressAddressLine1" : "1 VERONICA HOUSE", "RegAddressAddressLine2" : "WICKHAM ROAD", "RegAddressPostTown" : "BROCKLEY", "RegAddressCounty" : "", "RegAddressCountry" : "", "RegAddressPostCode" : "SE4 1NQ", "CompanyCategory" : "Private Limited Company", "CompanyStatus" : "Active", "CountryOfOrigin" : "United Kingdom", "DissolutionDate" : "", "IncorporationDate" : "12/05/1992", "SICCodeSicText_1" : "90010 - Performing arts", "URI" : "http://business.data.gov.uk/id/company/02714021" }
{ "_id" : { "$oid" : "5922005ccffbfad13f1f95e7" }, "Name" : "pending", "Position" : "pending", "Company" : "pending", "Description" : "pending", "Country" : "pending", "Zip" : "pending", "City" : "pending", "State" : "pending", "Address" : "pending", "Email" : "pending", "Website" : "pending", "Phonenumber" : "pending", "CompanyNumber" : { "$numberLong" : "10121672" }, "CompanyName" : "\" AS WE DO \" \" AS I DO \" LIMITED", "RegAddressAddressLine1" : "59A CRICKETFIELD ROAD", "RegAddressAddressLine2" : "", "RegAddressPostTown" : "LONDON", "RegAddressCounty" : "", "RegAddressCountry" : "ENGLAND", "RegAddressPostCode" : "E5 8NR", "CompanyCategory" : "Private Limited Company", "CompanyStatus" : "Active", "CountryOfOrigin" : "United Kingdom", "DissolutionDate" : "", "IncorporationDate" : "13/04/2016", "SICCodeSicText_1" : "None Supplied", "URI" : "http://business.data.gov.uk/id/company/10121672" }
{ "_id" : { "$oid" : "5922005ccffbfad13f1f95e8" }, "Name" : "pending", "Position" : "pending", "Company" : "pending", "Description" : "pending", "Country" : "pending", "Zip" : "pending", "City" : "pending", "State" : "pending", "Address" : "pending", "Email" : "pending", "Website" : "pending", "Phonenumber" : "pending", "CompanyNumber" : { "$numberLong" : "10275228" }, "CompanyName" : "\" BROOK VIEW \"MECHANICAL AND ELECTRICAL ENGINEERING SERVICES LIMITED", "RegAddressAddressLine1" : "33 BROOKLANDS ROAD", "RegAddressAddressLine2" : "", "RegAddressPostTown" : "HAVANT", "RegAddressCounty" : "HAMPSHIRE", "RegAddressCountry" : "UNITED KINGDOM", "RegAddressPostCode" : "PO9 3NT", "CompanyCategory" : "Private Limited Company", "CompanyStatus" : "Active", "CountryOfOrigin" : "United Kingdom", "DissolutionDate" : "", "IncorporationDate" : "12/07/2016", "SICCodeSicText_1" : "33200 - Installation of industrial machinery and equipment", "URI" : "http://business.data.gov.uk/id/company/10275228" }';
答案 0 :(得分:0)
json字符串不正确。将对象包装在一个数组中:
<?php
$json = '[{ "_id" : { "$oid" : "5922005ccffbfad13f1f95df" }, "Name" : "pending", "Position" : "pending", "Company" : "pending", "Description" : "pending", "Country" : "pending", "Zip" : "pending", "City" : "pending", "State" : "pending", "Address" : "pending", "Email" : "pending", "Website" : "pending", "Phonenumber" : "pending", "CompanyNumber" : 8209948, "CompanyName" : "! LTD", "RegAddressAddressLine1" : "METROHOUSE 57 PEPPER ROAD", "RegAddressAddressLine2" : "HUNSLET", "RegAddressPostTown" : "LEEDS", "RegAddressCounty" : "YORKSHIRE", "RegAddressCountry" : "", "RegAddressPostCode" : "LS10 2RU", "CompanyCategory" : "Private Limited Company", "CompanyStatus" : "Active", "CountryOfOrigin" : "United Kingdom", "DissolutionDate" : "", "IncorporationDate" : "11/09/2012", "SICCodeSicText_1" : "99999 - Dormant Company", "URI" : "http://business.data.gov.uk/id/company/08209948" },
{ "_id" : { "$oid" : "5922005ccffbfad13f1f95e0" }, "Name" : "pending", "Position" : "pending", "Company" : "pending", "Description" : "pending", "Country" : "pending", "Zip" : "pending", "City" : "pending", "State" : "pending", "Address" : "pending", "Email" : "pending", "Website" : "pending", "Phonenumber" : "pending", "CompanyNumber" : 7382019, "CompanyName" : "!BIG IMPACT GRAPHICS LIMITED", "RegAddressAddressLine1" : "335 ROSDEN HOUSE", "RegAddressAddressLine2" : "372 OLD STREET", "RegAddressPostTown" : "LONDON", "RegAddressCounty" : "", "RegAddressCountry" : "", "RegAddressPostCode" : "EC1V 9AV", "CompanyCategory" : "Private Limited Company", "CompanyStatus" : "Active", "CountryOfOrigin" : "United Kingdom", "DissolutionDate" : "", "IncorporationDate" : "21/09/2010", "SICCodeSicText_1" : "59112 - Video production activities", "URI" : "http://business.data.gov.uk/id/company/07382019" },
{ "_id" : { "$oid" : "5922005ccffbfad13f1f95e1" }, "Name" : "pending", "Position" : "pending", "Company" : "pending", "Description" : "pending", "Country" : "pending", "Zip" : "pending", "City" : "pending", "State" : "pending", "Address" : "pending", "Email" : "pending", "Website" : "pending", "Phonenumber" : "pending", "CompanyNumber" : 4753368, "CompanyName" : "!NFERNO LTD.", "RegAddressAddressLine1" : "FIRST FLOOR THAVIES INN HOUSE 3-4", "RegAddressAddressLine2" : "HOLBORN CIRCUS", "RegAddressPostTown" : "LONDON", "RegAddressCounty" : "", "RegAddressCountry" : "", "RegAddressPostCode" : "EC1N 2HA", "CompanyCategory" : "Private Limited Company", "CompanyStatus" : "Active - Proposal to Strike off", "CountryOfOrigin" : "United Kingdom", "DissolutionDate" : "", "IncorporationDate" : "03/05/2003", "SICCodeSicText_1" : "82990 - Other business support service activities n.e.c.", "URI" : "http://business.data.gov.uk/id/company/04753368" },
{ "_id" : { "$oid" : "5922005ccffbfad13f1f95e2" }, "Name" : "pending", "Position" : "pending", "Company" : "pending", "Description" : "pending", "Country" : "pending", "Zip" : "pending", "City" : "pending", "State" : "pending", "Address" : "pending", "Email" : "pending", "Website" : "pending", "Phonenumber" : "pending", "CompanyNumber" : "SC421617", "CompanyName" : "!NSPIRED LTD", "RegAddressAddressLine1" : "26 POLMUIR ROAD", "RegAddressAddressLine2" : "", "RegAddressPostTown" : "ABERDEEN", "RegAddressCounty" : "", "RegAddressCountry" : "UNITED KINGDOM", "RegAddressPostCode" : "AB11 7SY", "CompanyCategory" : "Private Limited Company", "CompanyStatus" : "Active", "CountryOfOrigin" : "United Kingdom", "DissolutionDate" : "", "IncorporationDate" : "11/04/2012", "SICCodeSicText_1" : "70229 - Management consultancy activities other than financial management", "URI" : "http://business.data.gov.uk/id/company/SC421617" },
{ "_id" : { "$oid" : "5922005ccffbfad13f1f95e3" }, "Name" : "pending", "Position" : "pending", "Company" : "pending", "Description" : "pending", "Country" : "pending", "Zip" : "pending", "City" : "pending", "State" : "pending", "Address" : "pending", "Email" : "pending", "Website" : "pending", "Phonenumber" : "pending", "CompanyNumber" : 9152972, "CompanyName" : "!NVERTD DESIGNS LIMITED", "RegAddressAddressLine1" : "32 RECTORY ROAD", "RegAddressAddressLine2" : "", "RegAddressPostTown" : "STEPPINGLEY", "RegAddressCounty" : "", "RegAddressCountry" : "", "RegAddressPostCode" : "MK45 5AT", "CompanyCategory" : "Private Limited Company", "CompanyStatus" : "Active", "CountryOfOrigin" : "United Kingdom", "DissolutionDate" : "", "IncorporationDate" : "30/07/2014", "SICCodeSicText_1" : "58190 - Other publishing activities", "URI" : "http://business.data.gov.uk/id/company/09152972" },
{ "_id" : { "$oid" : "5922005ccffbfad13f1f95e4" }, "Name" : "pending", "Position" : "pending", "Company" : "pending", "Description" : "pending", "Country" : "pending", "Zip" : "pending", "City" : "pending", "State" : "pending", "Address" : "pending", "Email" : "pending", "Website" : "pending", "Phonenumber" : "pending", "CompanyNumber" : "FC031362", "CompanyName" : "!OBAC LIMITED", "RegAddressAddressLine1" : "1ST AND 2ND FLOORS ELIZABETH HOUSE", "RegAddressAddressLine2" : "LES RUETIES BRAYES", "RegAddressPostTown" : "ST PETER PORT", "RegAddressCounty" : "GUERNSEY", "RegAddressCountry" : "GUERNSEY", "RegAddressPostCode" : "GY1 1EW", "CompanyCategory" : "Other company type", "CompanyStatus" : "Active", "CountryOfOrigin" : "CHANNEL ISLANDS", "DissolutionDate" : "", "IncorporationDate" : "30/11/2012", "SICCodeSicText_1" : "None Supplied", "URI" : "http://business.data.gov.uk/id/company/FC031362" },
{ "_id" : { "$oid" : "5922005ccffbfad13f1f95e5" }, "Name" : "pending", "Position" : "pending", "Company" : "pending", "Description" : "pending", "Country" : "pending", "Zip" : "pending", "City" : "pending", "State" : "pending", "Address" : "pending", "Email" : "pending", "Website" : "pending", "Phonenumber" : "pending", "CompanyNumber" : 7687209, "CompanyName" : "!OBAC UK LIMITED", "RegAddressAddressLine1" : "ENDEAVOUR HOUSE", "RegAddressAddressLine2" : "COOPERS END ROAD", "RegAddressPostTown" : "STANSTED AIRPORT", "RegAddressCounty" : "", "RegAddressCountry" : "", "RegAddressPostCode" : "CM24 1SJ", "CompanyCategory" : "Private Limited Company", "CompanyStatus" : "Active", "CountryOfOrigin" : "United Kingdom", "DissolutionDate" : "", "IncorporationDate" : "29/06/2011", "SICCodeSicText_1" : "70229 - Management consultancy activities other than financial management", "URI" : "http://business.data.gov.uk/id/company/07687209" },
{ "_id" : { "$oid" : "5922005ccffbfad13f1f95e6" }, "Name" : "pending", "Position" : "pending", "Company" : "pending", "Description" : "pending", "Country" : "pending", "Zip" : "pending", "City" : "pending", "State" : "pending", "Address" : "pending", "Email" : "pending", "Website" : "pending", "Phonenumber" : "pending", "CompanyNumber" : 2714021, "CompanyName" : "!YOZO FASS LIMITED", "RegAddressAddressLine1" : "1 VERONICA HOUSE", "RegAddressAddressLine2" : "WICKHAM ROAD", "RegAddressPostTown" : "BROCKLEY", "RegAddressCounty" : "", "RegAddressCountry" : "", "RegAddressPostCode" : "SE4 1NQ", "CompanyCategory" : "Private Limited Company", "CompanyStatus" : "Active", "CountryOfOrigin" : "United Kingdom", "DissolutionDate" : "", "IncorporationDate" : "12/05/1992", "SICCodeSicText_1" : "90010 - Performing arts", "URI" : "http://business.data.gov.uk/id/company/02714021" },
{ "_id" : { "$oid" : "5922005ccffbfad13f1f95e7" }, "Name" : "pending", "Position" : "pending", "Company" : "pending", "Description" : "pending", "Country" : "pending", "Zip" : "pending", "City" : "pending", "State" : "pending", "Address" : "pending", "Email" : "pending", "Website" : "pending", "Phonenumber" : "pending", "CompanyNumber" : { "$numberLong" : "10121672" }, "CompanyName" : "\" AS WE DO \" \" AS I DO \" LIMITED", "RegAddressAddressLine1" : "59A CRICKETFIELD ROAD", "RegAddressAddressLine2" : "", "RegAddressPostTown" : "LONDON", "RegAddressCounty" : "", "RegAddressCountry" : "ENGLAND", "RegAddressPostCode" : "E5 8NR", "CompanyCategory" : "Private Limited Company", "CompanyStatus" : "Active", "CountryOfOrigin" : "United Kingdom", "DissolutionDate" : "", "IncorporationDate" : "13/04/2016", "SICCodeSicText_1" : "None Supplied", "URI" : "http://business.data.gov.uk/id/company/10121672" },
{ "_id" : { "$oid" : "5922005ccffbfad13f1f95e8" }, "Name" : "pending", "Position" : "pending", "Company" : "pending", "Description" : "pending", "Country" : "pending", "Zip" : "pending", "City" : "pending", "State" : "pending", "Address" : "pending", "Email" : "pending", "Website" : "pending", "Phonenumber" : "pending", "CompanyNumber" : { "$numberLong" : "10275228" }, "CompanyName" : "\" BROOK VIEW \"MECHANICAL AND ELECTRICAL ENGINEERING SERVICES LIMITED", "RegAddressAddressLine1" : "33 BROOKLANDS ROAD", "RegAddressAddressLine2" : "", "RegAddressPostTown" : "HAVANT", "RegAddressCounty" : "HAMPSHIRE", "RegAddressCountry" : "UNITED KINGDOM", "RegAddressPostCode" : "PO9 3NT", "CompanyCategory" : "Private Limited Company", "CompanyStatus" : "Active", "CountryOfOrigin" : "United Kingdom", "DissolutionDate" : "", "IncorporationDate" : "12/07/2016", "SICCodeSicText_1" : "33200 - Installation of industrial machinery and equipment", "URI" : "http://business.data.gov.uk/id/company/10275228" }]';
$json = json_decode($json);
foreach ($json as $item) {
var_dump($item->_id);
// postToTheApi($item->_id);
}