I have some JSON data as follows:
{
"TABLE": [{
"ROW": [{
"COL": [{
"DATA": "Buff momo"
}, {
"DATA": "60.00"
}, {
"DATA": "1;#"
}, {
"DATA": "0"
}, {
"DATA": "1"
}, {
"DATA": "1"
}, {
"DATA": "118"
}, {
"DATA": "1;#{5D73B50D-2FFC-4D16-881E-D49328447AAB}"
}, {
"DATA": "1;#0"
}, {
"DATA": "2010-06-21 15:43:53"
}, {
"DATA": "Miscellaneous"
}, {
"DATA": "1;#Home/Lists/Canteen Menu/1_.000"
}]
}, {
"COL": [{
"DATA": "Chicken drumstick"
}, {
"DATA": "100.00"
}, {
"DATA": "3;#"
}, {
"DATA": "0"
}, {
"DATA": "1"
}, {
"DATA": "3"
}, {
"DATA": "40"
}, {
"DATA": "3;#{A1E0F087-57D3-4039-991B-E08B1CB3892A}"
}, {
"DATA": "3;#0"
}, {
"DATA": "2010-06-21 15:44:14"
}, {
"DATA": "Chinese"
}, {
"DATA": "3;#Home/Lists/Canteen Menu/3_.000"
}]
}
}]
}]
}]
}
""
I tried to decode it as follows:
header('Content-Type: application/json');
$json_string = utf8_encode(file_get_contents("filename"));
$parsed_json = json_decode($json_string, true);
foreach ($parsed_json as $key => $value) {
}
but to no avail. I am gettig errors as Invalid argument supplied for foreach().
Where am I doing wrong?
Your JSON is invalid. There are some brackets which do not belong there. The right JSON is
{ "TABLE":[{ "ROW":[ { "COL":[ {"DATA":"Buff momo"},{"DATA":"60.00"},{"DATA":"1;#"},{"DATA":"0"},{"DATA":"1"},{"DATA":"1"},{"DATA":"118"},{"DATA":"1;#{5D73B50D-2FFC-4D16-881E-D49328447AAB}"},{"DATA":"1;#0"},{"DATA":"2010-06-21 15:43:53"},{"DATA":"Miscellaneous"},{"DATA":"1;#Home/Lists/Canteen Menu/1_.000"}]}, { "COL":[ {"DATA":"Chicken drumstick"},{"DATA":"100.00"},{"DATA":"3;#"},{"DATA":"0"},{"DATA":"1"},{"DATA":"3"},{"DATA":"40"},{"DATA":"3;#{A1E0F087-57D3-4039-991B-E08B1CB3892A}"},{"DATA":"3;#0"},{"DATA":"2010-06-21 15:44:14"},{"DATA":"Chinese"},{"DATA":"3;#Home/Lists/Canteen Menu/3_.000"}]}]} ]}
you can test your JSON at http://json.parser.online.fr/
Use json_decode function for decoding json
*Updated : Example to show each COL as a list
<?php
$json_string = '{ "TABLE":[{ "ROW":[ { "COL":[ {"DATA":"Buff momo"},{"DATA":"60.00"},{"DATA":"1;#"},{"DATA":"0"},{"DATA":"1"},{"DATA":"1"},{"DATA":"118"},{"DATA":"1;#{5D73B50D-2FFC-4D16-881E-D49328447AAB}"},{"DATA":"1;#0"},{"DATA":"2010-06-21 15:43:53"},{"DATA":"Miscellaneous"},{"DATA":"1;#Home/Lists/Canteen Menu/1_.000"}]}, { "COL":[ {"DATA":"Chicken drumstick"},{"DATA":"100.00"},{"DATA":"3;#"},{"DATA":"0"},{"DATA":"1"},{"DATA":"3"},{"DATA":"40"},{"DATA":"3;#{A1E0F087-57D3-4039-991B-E08B1CB3892A}"},{"DATA":"3;#0"},{"DATA":"2010-06-21 15:44:14"},{"DATA":"Chinese"},{"DATA":"3;#Home/Lists/Canteen Menu/3_.000"}]}]} ]}';
$parsed_json = json_decode($json_string);
foreach ($parsed_json->TABLE[0]->ROW as $key => $value) {
echo 'SELECT ' . $key . ': <select name="select' . $key . '">';
foreach ($value->COL as $col) {
echo '<option value="' . $col->DATA . '">' . $col->DATA . '</option>';
}
echo '</select>';
}
?>
Related
i have a json file structured this way:
{
"data": {
"Category": {
"Product1": [{
"Code": "abc"
}],
"Product2": [{
"Code": "cba"
}]
},
"Category2": {
"Product3": [{
"Code": "abcabc"
}],
"Product4": [{
"Code": "cbacba"
}]
},
"Category3": {
"Product5": [{
"Code": "abcabcabc"
}],
"Product6": [{
"Code": "cbacbacba"
}]
}
}
}
How can i change the category names with a php script?
for example if i have 2 variables received from a form, called "$oldcategory" (which will be one of the existing categories) and "$newcategory" how can it, open the json file, place "$newcategory" where "$oldcategory" is and re-save the file?
<?php
$oldcategory= $_POST['oldcategory'];
$newcategory= $_POST['newcategory'];
$jsonString = file_get_contents('data.json');
$data = json_decode($jsonString, true);
...
foreach($data["data"] as $key=>$value){
$new = $data["data"][$oldcategory];
$data["data"][$newcategory]= $new ;
}
unset($data["data"][$oldcategory]);
$newJsonString = json_encode($data);
file_put_contents('data.json', $newJsonString);
?>
in case i was changing the "Category2" i'd like the result to be:
{
"data": {
"Category": {
"Product1": [{
"Code": "abc"
}],
"Product2": [{
"Code": "cba"
}]
},
"New_Category2": {
"Product3": [{
"Code": "abcabc"
}],
"Product4": [{
"Code": "cbacba"
}]
},
"Category3": {
"Product5": [{
"Code": "abcabcabc"
}],
"Product6": [{
"Code": "cbacbacba"
}]
}
}
}
instead of this:
{
"data": {
"Category": {
"Product1": [{
"Code": "abc"
}],
"Product2": [{
"Code": "cba"
}]
},
"Category3": {
"Product5": [{
"Code": "abcabcabc"
}],
"Product6": [{
"Code": "cbacbacba"
}]
},
"New_Category2": {
"Product3": [{
"Code": "abcabc"
}],
"Product4": [{
"Code": "cbacba"
}]
}
}
}
This was a possible solution:
foreach($data["data"] as $key=>$value){
if ($key == $oldcategory){
$key = $newcategory;}
$new["data"][$key] = $value;
}
$newJsonString = json_encode($new);
file_put_contents('data.json', $newJsonString);
This ended up being the solution to my problem:
<?php
$oldcategory= $_POST['oldcategory'];
$newcategory= $_POST['newcategory'];
$jsonString = file_get_contents('data.json');
$data = json_decode($jsonString, true);
...
foreach($data["data"] as $key=>$value){
if ($key == $oldcategory){
$key = $newcategory;}
$new["data"][$key] = $value;
}
$newJsonString = json_encode($new);
file_put_contents('data.json', $newJsonString);
?>
You could use a conditional, and create a new array with the desired values. When you reach the $oldcategory in your loop, replace the key with the $newcategory.
$newArray = [];
foreach($data["data"] as $key=>$value){
if($key == $oldcategory) {
$newArray[$newcategory] = $value;
} else {
$newArray[$key] = $value;
}
}
$newJsonString = json_encode($newArray);
I would like to know how to display the information below.
Link (JSON):
https://my.callofduty.com/api/papi-client/ce/v1/title/bo4/platform/psn/match/11337378706913618925/matchMapEvents
What I wish to have:
"teams": [
[
{
"provider": "psn",
"username": "Germania1992"
},
{
"provider": "psn",
"username": "killzoneprofi"
},
{
"provider": "psn",
"username": "ayozetf87"
},
{
"provider": "psn",
"username": "Seith911"
},
{
"provider": "psn",
"username": "domibreu92"
}
],
[
{
"provider": "psn",
"username": "Thejuankarboy"
},
{
"provider": "psn",
"username": "Gamermad101"
},
{
"provider": "psn",
"username": "Izdrap"
},
{
"provider": "psn",
"username": "Guerra_sv"
},
{
"provider": "psn",
"username": "TriX_FollOoW_YT"
}
]
],
I want to display the nicks of the different teams
Example: Team 1 = Germania1992, killzoneprofi, ayozetf87, Seith911,
domibreu92
Thanks
You will have to do a double foreach that for sure.
<?php
// Get the json of the team
$team = team("https://my.callofduty.com/api/papi-client/ce/v1/title/bo4/platform/psn/match/11337378706913618925/matchMapEvents");
// Display the teams info
foreach($team as $nb=>$data){
echo "Team $nb<br />";
foreach($data as $key=>$value){
echo "Provider: ".$value->provider."<br />";
echo "Username: ".$value->username."<br />";
}
echo "<hr>";
}
// Returns the team array of the json
function team($jsonURL){
$content=file_get_contents($jsonURL);
$data=json_decode($content);
return $data->data->teams;
}
?>
You could increment the team number by 1 to avoid the first being 0
The above will display the below screenshot (You can format the output as you please)
$result = [];
$counter = 0;
dump($array = json_decode(file_get_contents('https://my.callofduty.com/api/papi-client/ce/v1/title/bo4/platform/psn/match/11337378706913618925/matchMapEvents')));
dump($t = array_column((array)$array, 'teams'));
foreach ($t as $r) {
foreach ($r as $p) {
$counter++;
foreach ($p as $value){
$result["team$counter"][] = $value->username;
}
}
}
var_dump($result);
I'm trying to parse data returned by the Jotform API.
I'm successfully echoing the data but I'm also getting unnecessary additional lines of text.
My PHP code is:
include "JotForm.php";
$jotformAPI = new JotForm("myapikey");
$submissions = $jotformAPI->getFormSubmissions("myformid");
var_dump($submissions );
foreach ($submissions as $data) {
$detail = $jotformAPI->getSubmission($data['id']);
foreach ($detail as $d) {
echo $d[1]['answer']['first'] . '<br>';
}
}
result of var_dump($submissions );
{
"responseCode": 200,
"message": "success",
"content": [{
"id": "237955080346633702",
"form_id": "31751954731962",
"ip": "123.123.123.123",
"created_at": "2013-06-25 03:38:00",
"updated_at": "2013-06-27 04:58:00",
"status": "ACTIVE",
"new": "1",
"answers": {
"1": {
"text": "Name",
"type":"control_fullname",
"answer": {
"first": "LeBron",
"last": "James"
},
"prettyFormat": "LeBron James"
},
"2": {
"text": "Your Message",
"type": "control_textarea",
"answer":"¡Ay, caramba!"
}
}],
}
And here is the result I'm getting:
1
0
0
0
C
0
LeBron
I had to repair your invalid json string...
Code: (Demo)
$json = '{
"responseCode": 200,
"message": "success",
"content": [{
"id": "237955080346633702",
"form_id": "31751954731962",
"ip": "123.123.123.123",
"created_at": "2013-06-25 03:38:00",
"updated_at": "2013-06-27 04:58:00",
"status": "ACTIVE",
"new": "1",
"answers": {
"1": {
"text": "Name",
"type":"control_fullname",
"answer": {
"first": "LeBron",
"last": "James"
},
"prettyFormat": "LeBron James"
},
"2": {
"text": "Your Message",
"type": "control_textarea",
"answer":"¡Ay, caramba!"
}
}}]
}';
foreach (json_decode($json, true)['content'] as $set) {
echo "{$set['answers'][1]['answer']['first']} {$set['answers'][1]['answer']['last']}\n";
}
Output:
LeBron James
I'm a little fuzzy on the action of the jot functions, but maybe it's supposed to be like this:
foreach ($submissions as $data) {
$detail = $jotformAPI->getSubmission($data['id']);
echo $detail['answers'][1]['answer']['first'] . '<br>';
}
This is my php file code where i am fetching data from thecatapi
i needed the specific format for datatable
$xml=file_get_contents('https://thecatapi.com/api/images/get?format=xml&results_per_page=3');
$xml = new SimpleXMLElement($xml);
$datas=array();
$datas['rows'] = $xml->data->images;
echo json_encode($datas, true);
Output is:
{"total":50,
"rows":{"image":[
{"url":"https:\/\/thecatapi.com\/api\/images\/get.php?id=e0i",
"id":"e0i",
"source_url":"http:\/\/thecatapi.com\/?id=e0i"
},
{"url":"https:\/\/thecatapi.com\/api\/images\/get.php?id=MTYwNDE0MQ",
"id":"MTYwNDE0MQ",
"source_url":"http:\/\/thecatapi.com\/?id=MTYwNDE0MQ"
},{
"url":"https:\/\/thecatapi.com\/api\/images\/get.php?id=bon",
"id":"bon",
"source_url":"http:\/\/thecatapi.com\/?id=bon"
}
]
}
}
but i wanted in this json form
{
"total": 800,
"rows": [
{
"url": 0,
"id": "Item 0",
"source_url": "$0"
},
{
"url": 0,
"id": "Item 0",
"source_url": "$0"
},
{
"url": 0,
"id": "Item 0",
"source_url": "$0"
},
Just came up with an alternativ solution. Change this line:
$datas['rows'] = $xml->data->images;
to this:
$datas['rows'] = $xml->data->images->image;
otherwise you can still do this
Decode the json string into an object and modify it suit your needs.
$stdClassObject = json_decode($jsonData);
$stdClassObject->rows = $stdClassObject->rows->image;
$newJsonData = json_encode($stdClassObject, JSON_PRETTY_PRINT);
echo '<pre>';
print_r($newJsonData);
echo '</pre>';
New output:
{
"total": 50,
"rows": [
{
"url": "https:\/\/thecatapi.com\/api\/images\/get.php?id=e0i",
"id": "e0i",
"source_url": "http:\/\/thecatapi.com\/?id=e0i"
},
{
"url": "https:\/\/thecatapi.com\/api\/images\/get.php?id=MTYwNDE0MQ",
"id": "MTYwNDE0MQ",
"source_url": "http:\/\/thecatapi.com\/?id=MTYwNDE0MQ"
},
{
"url": "https:\/\/thecatapi.com\/api\/images\/get.php?id=bon",
"id": "bon",
"source_url": "http:\/\/thecatapi.com\/?id=bon"
}
]
}
I have web page which get Json responce like this
{
'hotel_1page':[
{
'id':'10',
'name':'fsf',
'telephone':'233333'
},
{
'id':'11',
'name':'setttttt',
'telephone':'213123123'
},
{
'id':'12',
'name':'fsdfsdf',
'telephone':'122212121'
},
{
'id':'13',
'name':'xxcvcxv',
'telephone':'2147483647'
},
{
'id':'14',
'name':'dssdfg',
'telephone':'2147483647'
},
{
'id':'15',
'name':'dfsdfsdf',
'telephone':'21312321'
},
{
'id':'16',
'name':'fx_test_nw1',
'telephone':'23232323'
},
{
'id':'17',
'name':'fx_test_nw2',
'telephone':'31313131'
}
]
}
and i want to loop through this data and get this data to array and display as html how can i achieve this
this is where i get get json responce,and when i var_dump json encoded variable it says null,where $json variable shows the json responce,
$json = file_get_contents('http://www.example.com/hotel_list.php');
var_dump($json);
$array = json_decode($json, true);
var_dump($array);
The json that you are trying to parse is invalid. Change the single quotes to double quotes and you should be able to parse it to an array.
The JSON documentation says
A value can be a string in double quotes, or a number, or true or false or null, or an object or an array. These structures can be nested.
This should work:
$json = '{ "hotel_1page": [ { "id": "10", "name": "fsf", "telephone": "233333" }, { "id": "11", "name": "setttttt", "telephone": "213123123" }, { "id": "12", "name": "fsdfsdf", "telephone": "122212121" }, { "id": "13", "name": "xxcvcxv", "telephone": "2147483647" }, { "id": "14", "name": "dssdfg", "telephone": "2147483647" }, { "id": "15", "name": "dfsdfsdf", "telephone": "21312321" }, { "id": "16", "name": "fx_test_nw1", "telephone": "23232323" }, { "id": "17", "name": "fx_test_nw2", "telephone": "31313131" } ] }';
echo '<pre>';
var_dump(json_decode($json, true));
echo '</pre>';
I replaced all single quotes with double quotes, that did the trick.
I used single quotes before which caused json_decode to return null
[Edit for usage]
To use the data, you could use something like this:
$obj = json_decode($json, true);
foreach($obj as $row)
{
foreach($row as $key => $item)
{
// $item['id']
// $item['name']
// $item['telephone']
}
}
I'm using a nested foreach() because the id, name and telephone are an array within the hotel_page1 array