I've been having issues trying to parse this json data.
I tried to do this to return the name but its not working:
foreach(json_decode($test) as $item){
$name= $item->users->name;}
This is the json code:
{
"users":[
{
"id":"dsfdfsd",
"id_str":"dsfsdf",
"name":"Davy",
"screen_name":"Davy232",
"location":"Colorado"
},
{
"id":"wer",
"id_str":"wer",
"name":"Sarah",
"screen_name":"Davy232",
"location":"LA"
},
{
"id":"fdf",
"id_str":"fdf",
"name":"James",
"screen_name":"James374",
"location":"Vegas"
}
]
}
That is because the JSON is invalid , Here's the proper fixed JSON
Fixed JSON Data
{
"users":[
{
"id":"dsfdfsd",
"id_str":"dsfsdf",
"name":"Davy",
"screen_name":"Davy232",
"location":"Colorado"
}
]
}
What were the problems ?
You did not surround dsfdfsd with doubles quotes.
There was an extra comma after Colorado
The braces were not properly balanced.
Also, your foreach should be like this..
foreach(json_decode($test) as $item){
echo $item[0]->name;
}
Working Demo - Part 1
Working Demo - Part 2
Related
i have a pretty weird json here. Im trying to iterate through every subscription object and get some data
Here is the json
{
"result":"success",
"data":{
"subscriptions":[
{
"name":"app_generator",
"expirey":1628188363942,
"hwids":3,
"hwids_used":[
"dc253a62ea65cd92c6cb26f15330cf4a8f9e5c3ecf7caae161a3f97de4c84515",
"efede5757f512f9eae1d600302ca5c4e6a3cadb0658310c91c6da2ada68175f4"
]
},
{
"name":"apple",
"expirey":1625596518277,
"hwids":2,
"hwids_used":[
"dc253a62ea65cd92c6cb26f15330cf4a8f9e5c3ecf7caae161a3f97de4c84515",
"dc253a62ea65cd92c6cb26f15330cf4a8f9e5c3ecf7caae161a3f97de4c84515a"
]
}
]
}
}
The code i tried was:
foreach($json['result']['data']['subscriptions'] as $a)
{
//print_r($a);
$expirey = $a['expirey']
}
Im just getting a lot of errors. Let me know what the correct approach would be. My goal is to mainly get the name & expirey data
I know this is a basic question, but I cannot figure out how to actually do this. I have read countless tutorials about it but they seemingly do not work.
var_dump($google_check);
returns the following:
string(488) "{
"matches": [
{
"threatType": "MALWARE",
"platformType": "LINUX",
"threat": {
"url": "http://malware.testing.google.test/testing/malware/"
},
"cacheDuration": "300s",
"threatEntryType": "URL"
},
{
"threatType": "MALWARE",
"platformType": "LINUX",
"threat": {
"url": "http://malware.testing.google.test/testing/malware/"
},
"cacheDuration": "300s",
"threatEntryType": "URL"
}
]
}
"
I want to echo results from the array, so something like
echo $google_check[0][matches][threat];
echo $google_check[1][matches][threat];
Problem is this returns illegal offset on matches and threat, and only echo's a single character {
What am I doing wrong? How do I echo the results from this array without dumping the entire array?
The response you recieved is in json so you'll need to first json_decode the response.
$decoded = json_decode($google_check, true);
Then you can access it like an array
echo $decoded['matches'][0]['threat'];
echo $decoded['matches'][1]['threat'];
if you want the url value you'll need to do it like this.
echo $decoded['matches'][0]['threat']['url'];
echo $decoded['matches'][1]['threat']['url'];
please also note, when looking at array keys that aren't numerical you'll need to wrap in quotes (e.g. $decoded['matches'] instead of $decoded[matches]).
Here's a quick explanation on json
https://www.tutorialspoint.com/json/json_php_example.htm
I need to parse a JSON file. I've only worked with XML before.
How can I get the second "food_id" (1730905)?
Here is my JSON file:
{
"shopId":29,
"last":46977914,
"freshfood":[
{
"freshfood_id":2629,
"food":[
{
"food_id":1740851,
"type":"fruit",
"status":1
},
{
"food_id":1730905,
"type":"vegetable",
"status":1
},
]
}
]
}
I tried this, but it does not work.
$string = file_get_contents("food.json");
$json_a=json_decode($string,true);
echo $GetFreshFoodId = $json_a['freshfood'][1]['freshfood_id'];
PHP arrays are zero-based, so that should be:
$json_a['freshfood'][0]['food'][1]['food_id'];
Also, note that the JSON is not entirely valid - you should remove the last comma. (But you might have left out additional records in your example JSON for clarity.)
Here is my Json. This ia an dwr response ( a webservice created in java) .
{
key1 : {
date : "Today" ,
items : [
{
itemKey1 : "itemValue1",
itemKey2 : "itemValue2",
},
{
itemKey1 : "itemValue1",
itemKey2 : "itemValue2",
},
]
}
}
JSON LINT is also showing the error.
If you can see key do not have "" may be that's why i can not parse it to json in php directly. Is there any way oi can parse it to json and then to array or directly to an array.
But when i transfor this to this type of json it. In JSON LINT it shows that it is proper json.
{
"key1": {
"date": "Today",
"items": [
{
"itemKey1": "itemValue1",
"itemKey2": "itemValue2"
},
{
"itemKey1": "itemValue1",
"itemKey2": "itemValue2"
}
]
}
}
So is there anyway i can trasnfer json to second type. Dynamically in PHP
Since there is no javascript parser build in to PHP and what you have here is JAVASCRIPT and not JSON, your only options is really to implement your own parser / use an existing parser. OR wrangle your string into being JSON, this COULD be done with something like regex, though it will most likely be flaky.
For your specified example data, this would do:
<?php
$data = json_decode(preg_replace_callback('#(\S+)\s*:#i', function($matches){
return '"'.$matches[1].'" :';
},$str));
HI im having a little difficulty with dataTables and php. I'm echoing out json in the format below:
{"iTotalRecords":10,"iTotalDisplayRecords":10,"aaData":[[ "1", "15","1","long description long description long description long description"," 2012-02-25 00:00:00"],[ "1", "15","1","long description long description long description long description"," 2012-02-25 00:18:59"] ... ] }
Which inst working with my dataTable, However after validating the above in jsonlint.com/, i get the well formated version below:
{
"iTotalRecords": 10,
"iTotalDisplayRecords": 10,
"aaData": [
[
"1",
"15",
"1",
"long description long description long description long description",
"2012-02-25 00:18:59"
],
...
]
}
When I put this in a txt file it loads just fine. I also noticed that adding a line break in the "long description" part, it also doesn't work even with the one above. My guess is that the line break is messing with the format of the json, but how can I avoid this in my php script since everything is being word wrapped? I've tried \n in my echo code but it doesnt seem to create a newline.
The opening and closing curly braces { and } are not acceptable to DataTables. My guess is, it's considered an object rather than an array. Try this:
Use the following php function to create your JSON array:
$this->arrayJSON = $this->arrayPHPToJS($myArray);
public function arrayPHPToJS($phpArray) {
if (is_null($phpArray)) return 'null';
if (is_string($phpArray)) return "'" . $phpArray . "'";
if (self::is_assoc($phpArray)) {
$a=array();
foreach ($phpArray as $key => $val )
$a[]=self::arrayPHPtoJS($val);
return "[" . implode ( ', ', $a ) . "]";
}
if (is_array($phpArray)) {
$a=array();
foreach ($phpArray as $val )
$a[]=self::arrayPHPtoJS($val);
return "[" . implode ( ', ', $a ) . "]";
}
return json_encode($phpArray);
}
Use this array in JS script in your view (make sure there's no html escaping):
<script type="text/javascript">
var jsArray = <?php echo $arrayJSON; ?>;
$(function(){
var oTable = $('#mytable').dataTable( {
"aaData": jsArray,
....
});
});
</script>
sounds more like the number of columns set up in table/datatables doesn't match number of items in your json array
There seemsed to be an issue with one of my database values, it was starting on a new line, which cause the format of the json to be incorrect.