【发布时间】:2017-05-21 22:42:50
【问题描述】:
我正在寻找一种通过 REST api 从 mongodb json 导出中更新数据库的方法。
本质上,我想在这个 jsonfile 中查找“_id”对象,我希望通过POST 'someurl/thisapi' POST 到其余的 api
我尝试了多种使用 php 循环的方法,但似乎无法掌握处理对象的正确方法。
JSON 导出示例
$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" }';
尝试了以下方法,但没有成功
$string = file_get_contents("/home/michael/test.json");
$json_a=json_decode($string,true);
foreach ($json_a as $key => $value){
echo $key . ':' . $value;
}
【问题讨论】:
-
这看起来像是来自
mongoexport的数据 使用--jsonArray执行 mongoexport 以将其格式化为可以通过您自己的 JSON 解析器运行的方式。交替编写您自己的文件解析器并在“换行符”处中断并将每一行解析为有效的 JSON 对象,而不是一次读取内存中的整个文件。 -
天啊。传奇。我知道我错过了一些非常愚蠢的东西。我只是看不到它。谢谢!