Parsing a JSON file with PHP
Clash Royale CLAN TAG#URR8PPP
Parsing a JSON file with PHP
I'm trying to get data from the following JSON file using PHP. I specifically want the @name
and additional parameters in the below hierarchy, parsed and put in a tabular format which can be easily read as html.
@name
Considering we may be many types and sub-types wanted to understand the way it could be achieved in php, interactively.
It's probably really simple, but I have no idea how to do this. I'm stuck on what to do after file_get_contents("output.json")
file_get_contents("output.json")
"http://pmd.sourceforge.net/report/2.0.0pmd":
"@http://www.w3.org/2001/XMLSchema-instanceschemaLocation": "http://pmd.sourceforge.net/report/2.0.0 http://pmd.sourceforge.net/report_2_0_0.xsd",
"@version": "6.4.0",
"@timestamp": "2018-06-19T07:50:03.152",
"http://pmd.sourceforge.net/report/2.0.0file": [
"@name": "/opt/folder/file.cls",
"http://pmd.sourceforge.net/report/2.0.0violation": [
"@rule": "AvoidGlobalModifier",
"@priority": "3",
"@externalInfoUrl": "https://rule.html",
"@endcolumn": "2",
"@ruleset": "Best Practices",
"@begincolumn": "30",
"@beginline": "21",
"@endline": "313",
"#tail": "n",
"#text": "nAvoid using global modifiern"
,
"@rule": "StdCyclomaticComplexity",
"@priority": "3",
"@externalInfoUrl": "https://rule.html",
"@endcolumn": "2",
"@ruleset": "Design",
"@begincolumn": "30",
"@beginline": "21",
"@endline": "313",
"#tail": "n",
"#text": "nThe class 'Class' has a Standard Cyclomatic Complexity of 5 (Highest = 18).n"
,
"@rule": "ExcessiveParameterList",
"@priority": "3",
"@externalInfoUrl": "https://rule.html",
"@endcolumn": "6",
"@ruleset": "Design",
"@begincolumn": "29",
"@beginline": "219",
"@endline": "242",
"#tail": "n",
"#text": "nAvoid long parameter listsn"
,
"@rule": "Violation",
"@priority": "3",
"@externalInfoUrl": "https://rule.html",
"@endcolumn": "14",
"@ruleset": "Security",
"@begincolumn": "16",
"@beginline": "252",
"@endline": "264",
"#tail": "n",
"#text": "nValidate CRUD permission before SOQL/DML operationn"
],
"#tail": "n",
"#text": "n"
,
"@name": "/opt/folder/file2.cls",
"http://pmd.sourceforge.net/report/2.0.0violation":
"@rule": "CRUDViolation",
"@priority": "3",
"@externalInfoUrl": "https://rule.html",
"@endcolumn": "148",
"@ruleset": "Security",
"@begincolumn": "73",
"@beginline": "15",
"@endline": "15",
"#tail": "n",
"#text": "nValidate CRUD permission before SOQL/DML operationn"
,
"#tail": "n",
"#text": "n"
],
"#text": "n"
Thanks @YvesLeBorg . The Json is now validated with an extra '}' in the end. Somehow it was removed while pasting.
– Ashish Kaul
Aug 5 at 18:19
1 Answer
1
To parse json I. PHP you be to use json_decode.
$content =file_get_contents("http://example.com/ex.json");
$jsonContent = json_decode($content,true)
$jsonContent contain your json data into an array thanks to second parameters.
This is a quick example you ve to check if content is not empty and if there is a json error.
Hope this helps
EDIT Sorry i was on my phone..
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
search for and read about json_decode. Make certain your json is valid (hint : this one is not).
– YvesLeBorg
Aug 5 at 18:15