I'm trying to parse this JSON object and iterate through it to make a table in html. At the trials, I can't property echo the values. How do I do that correctly?
{"1":{"1":"Employer+EID","2":"File+Creation+Date","3":"File+Creation+Time","4":"Payer+EID","5":"Payer+QID","6":"Payer+Bank+Short+Name","7":"Payer+IBAN","8":"Salary+Year+and+Montd","9":"Total+Salaries","10":"Total+records","11":"","12":"","13":"","14":"","15":""},"2":{"1":"12435800","2":"20160714","3":"0318","4":"12435800","5":"","6":"DBQ","7":"QA79DOHB021104613880010010000","8":"201606","9":"183941.22166664","10":"113","11":"","12":"","13":"","14":"","15":""},"3":{"1":"Record+ID","2":"Employee+QID","3":"Employee+Visa+ID","4":"Employee+Name","5":"Employee+Bank+Short+Name","6":"Employee+Account","7":"Salary+Frequency","8":"Number+of+Working+Days","9":"Net+Salary","10":"Basic+Salary","11":"Extra+hours","12":"Extra+Income","13":"Deductions","14":"Payment+Type","15":"Notes/+Comments"},"4":{"1":"1","2":"27835620341","3":"","4":"SHIJAN+THARAKAN+THOMAS","5":"DBQ","6":"2025","7":"M","8":"30","9":"7300","10":"5000","11":"0.00","12":"2500.00000000","13":"200","14":"","15":""}}
I tried:
$data = json_decode($_POST['data'],true);
//echo count($data);
echo ($data[4][2]);`
The result was a null JSON. How can I do this correctly?
Your JSON String is -
$str = '{"1":{"1":"Employer+EID","2":"File+Creation+Date","3":"File+Creation+Time","4":"Payer+EID","5":"Payer+QID","6":"Payer+Bank+Short+Name","7":"Payer+IBAN","8":"Salary+Year+and+Montd","9":"Total+Salaries","10":"Total+records","11":"","12":"","13":"","14":"","15":""},"2":{"1":"12435800","2":"20160714","3":"0318","4":"12435800","5":"","6":"DBQ","7":"QA79DOHB021104613880010010000","8":"201606","9":"183941.22166664","10":"113","11":"","12":"","13":"","14":"","15":""},"3":{"1":"Record+ID","2":"Employee+QID","3":"Employee+Visa+ID","4":"Employee+Name","5":"Employee+Bank+Short+Name","6":"Employee+Account","7":"Salary+Frequency","8":"Number+of+Working+Days","9":"Net+Salary","10":"Basic+Salary","11":"Extra+hours","12":"Extra+Income","13":"Deductions","14":"Payment+Type","15":"Notes/+Comments"},"4":{"1":"1","2":"27835620341","3":"","4":"SHIJAN+THARAKAN+THOMAS","5":"DBQ","6":"2025","7":"M","8":"30","9":"7300","10":"5000","11":"0.00","12":"2500.00000000","13":"200","14":"","15":""}}';
$result_array = json_decode($str, true);
echo '<pre>'; print_r($result_array);
echo $result_array[4][2];
It works fine for me. Please check you $_POST['data'] variable. It may undefined debug this variable by print_r($_POST); if this data index is defined you will see an index of data such as -
Array
(
['data'] => 'some_value',
)
Related
I have a string on my databas, that I'm trying to insert into a json object as an Array, not a string..:
$arrayInString = "[2,3,5,5,6]"; // this comes from the database
$jsonObject = array('numbers' => $arrayInString);
$json = json_encode($jsonObject, JSON_UNESCAPED_SLASHES);
echo $json;
When I execute this.. my Json object is..
numbers: "[2,3,5,5,6]";
and not
numbers: [2,3,5,5,6];
Like I originally wanted.. can't get this to work, can anyone help?
Like was said you need to decode the passed data from the database and then build your array output. Something like this:
$arrayInString = "[2,3,5,5,6]"; // this comes from the database
// decode the JSON from the database as we build the array that will be converted back to json
$jsonObject = array('numbers' => json_decode($arrayInString));
echo json_encode($jsonObject, JSON_UNESCAPED_SLASHES);
The slightly modified code above outputs:
{"numbers":[2,3,5,5,6]}
You need to json_decode your $arrayInString variable before you add it to the associative array.
I've got a problem. I can't find a proper array in this json file. When i try this:
<?php
$jsonurl = "http://steamcommunity.com/profiles/76561198132044757/inventory/json/304930/2";
$json = file_get_contents($jsonurl);
$arJson = json_decode( $json, true );
echo $arJson[0]
?>
it says:
Notice: Undefined offset: 0
or if i try this:
echo $arJson["rgInventory"]
it says:
Notice: Array to string conversion
Where are the arrays in my json file and how to adress them?
Thank you in advance and sorry for my bad english ;)
Jonas
Notice: Undefined offset: 0
You get this error because your array is an associative array, that mean's there is no index 0. For more info take a look at the docs.
Notice: Array to string conversion
You get this error, because echo can only output strings not arrays
to see what's in your array, you can use var_dump()
<?php
$jsonurl = "http://steamcommunity.com/profiles/76561198132044757/inventory/json/304930/2";
$json = file_get_contents($jsonurl);
$arJson = json_decode( $json, true );
echo "<pre>";
var_dump( $arJson );
$arJson["rgInventory"] is also an array so you can see the values with:
var_dump( $arJson["rgInventory"] );
1) First of all, you should read Steam API documentation to know the structure of coming data.
https://steamcommunity.com/dev
2) Use print_r and var_dump functions to see the structure of your variables.
For example <pre> <? print_r($arJson) ?>
you have to use print_r in order to print an array.
See your result with
echo '<pre>';print_r($arJson);
I have following piece of code which is generating JSON variable. I use php built in json_decode function to decode the json variable but i am getting NULL after decoding JSOn variable.
$a=array("targetAction"=>"getHeadFields","targetHead"=>$table_name);
$obj1 = Post_Uamsdata($a);
echo $obj1;
$file = json_decode($obj1,true);
var_dump($file);
$obj1 is my json variable. whenever i echo it i get the result as follows:
{"success":"yes","error":"","message":"","arguments":"[{\"fieldNo\":\"1\",\"fieldName\":\"ItemType\",\"fieldType\":\"character(16)\",\"notnull\":\"f\",\"fieldLabel\":null,\"primary_key\":\"f\",\"default\":null,\"fieldOption\":[]},{\"fieldNo\":\"2\",\"fieldName\":\"Long\",\"fieldType\":\"character(20)\",\"notnull\":\"f\",\"fieldLabel\":null,\"primary_key\":\"f\",\"default\":null,\"fieldOption\":[]},{\"fieldNo\":\"3\",\"fieldName\":\"Lat\",\"fieldType\":\"character(20)\",\"notnull\":\"f\",\"fieldLabel\":null,\"primary_key\":\"f\",\"default\":null,\"fieldOption\":[]},{\"fieldNo\":\"4\",\"fieldName\":\"MapDate\",\"fieldType\":\"character(16)\",\"notnull\":\"f\",\"fieldLabel\":null,\"primary_key\":\"f\",\"default\":null,\"fieldOption\":[]},{\"fieldNo\":\"5\",\"fieldName\":\"FieldNote\",\"fieldType\":\"character(64)\",\"notnull\":\"f\",\"fieldLabel\":null,\"primary_key\":\"f\",\"default\":null,\"fieldOption\":[]}]"}
i have checked it in online json validator and it is saying that this JSOn is valid. But whenever i decode this $obj1 into $file then i am always getting NULL.
after json_decode we get the following, But I guess this is not what you want. You want the fieldNo, fieldName etc also to be parsed.
$json = '{"success":"yes","error":"","message":"","arguments":"[{\"fieldNo\":\"1\",\"fieldName\":\"ItemType\",\"fieldType\":\"character(16)\",\"notnull\":\"f\",\"fieldLabel\":null,\"primary_key\":\"f\",\"default\":null,\"fieldOption\":[]},{\"fieldNo\":\"2\",\"fieldName\":\"Long\",\"fieldType\":\"character(20)\",\"notnull\":\"f\",\"fieldLabel\":null,\"primary_key\":\"f\",\"default\":null,\"fieldOption\":[]},{\"fieldNo\":\"3\",\"fieldName\":\"Lat\",\"fieldType\":\"character(20)\",\"notnull\":\"f\",\"fieldLabel\":null,\"primary_key\":\"f\",\"default\":null,\"fieldOption\":[]},{\"fieldNo\":\"4\",\"fieldName\":\"MapDate\",\"fieldType\":\"character(16)\",\"notnull\":\"f\",\"fieldLabel\":null,\"primary_key\":\"f\",\"default\":null,\"fieldOption\":[]},{\"fieldNo\":\"5\",\"fieldName\":\"FieldNote\",\"fieldType\":\"character(64)\",\"notnull\":\"f\",\"fieldLabel\":null,\"primary_key\":\"f\",\"default\":null,\"fieldOption\":[]}]"}';
$arr = json_decode($json, true);
echo "<pre>";
print_r($arr);
Output is as follows
Array
(
[success] => yes
[error] =>
[message] =>
[arguments] => [{"fieldNo":"1","fieldName":"ItemType","fieldType":"character(16)","notnull":"f","fieldLabel":null,"primary_key":"f","default":null,"fieldOption":[]},{"fieldNo":"2","fieldName":"Long","fieldType":"character(20)","notnull":"f","fieldLabel":null,"primary_key":"f","default":null,"fieldOption":[]},{"fieldNo":"3","fieldName":"Lat","fieldType":"character(20)","notnull":"f","fieldLabel":null,"primary_key":"f","default":null,"fieldOption":[]},{"fieldNo":"4","fieldName":"MapDate","fieldType":"character(16)","notnull":"f","fieldLabel":null,"primary_key":"f","default":null,"fieldOption":[]},{"fieldNo":"5","fieldName":"FieldNote","fieldType":"character(64)","notnull":"f","fieldLabel":null,"primary_key":"f","default":null,"fieldOption":[]}]
)
As http://php.net/manual/en/function.json-decode.php indicates that the function will only work for UTF-8 encoded string, try the following:
$file = json_decode(mb_convert_encoding($obj1, 'UTF-8'),true);
I've have this list of products in JSON that needs to be decoded:
"[{"productId":"epIJp9","name":"Product A","amount":"5","identifier":"242"},{"productId":"a93fHL","name":"Product B","amount":"2","identifier":"985"}]"
After I decode it in PHP with json_decode(), I have no idea what kind of structure the output is. I assumed that it would be an array, but after I ask for count() it says its "0". How can I loop through this data so that I get the attributes of each product on the list.
Thanks!
To convert json to an array use
json_decode($json, true);
You can use json_decode() It will convert your json into array.
e.g,
$json_array = json_decode($your_json_data); // convert to object array
$json_array = json_decode($your_json_data, true); // convert to array
Then you can loop array variable like,
foreach($json_array as $json){
echo $json['key']; // you can access your key value like this if result is array
echo $json->key; // you can access your key value like this if result is object
}
Try like following codes:
$json_string = '[{"productId":"epIJp9","name":"Product A","amount":"5","identifier":"242"},{"productId":"a93fHL","name":"Product B","amount":"2","identifier":"985"}]';
$array = json_decode($json_string);
foreach ($array as $value)
{
echo $value->productId; // epIJp9
echo $value->name; // Product A
}
Get Count
echo count($array); // 2
Did you check the manual ?
http://www.php.net/manual/en/function.json-decode.php
Or just find some duplicates ?
How to convert JSON string to array
Use GOOGLE.
json_decode($json, true);
Second parameter. If it is true, it will return array.
You can try the code at php fiddle online, works for me
$list = '[{"productId":"epIJp9","name":"Product A","amount":"5","identifier":"242"},{"productId":"a93fHL","name":"Product B","amount":"2","identifier":"985"}]';
$decoded_list = json_decode($list);
echo count($decoded_list);
print_r($decoded_list);
JSON is like
{
"#http_status_code": 200,
"#records_count": 200,
"warnings": [],
"query": { ... ...
In PHP
$data = json_decode($json_entry);
print $data->#http_status_code; //returns error
print $data->http_status_code; //returns nothing
How can I retrieve status code?
1) As Object way
$data = json_decode($json_entry);
print $data->{'#http_status_code'};
2) OR use as array way by passing second argument as true in json_decode
$data = json_decode($json_entry, true);
print $data['#http_status_code'];
Try json_decode to get the json in form of array ..
$json_array = json_decode($data, true);
$required_data = $data['required_key']
with reference to your particular problem .. you will get array as
Array
(
[#http_status_code] => 200
[#records_count] => 200
[warnings] => Array
(
)
....
)
so you can access you data as $data['#http_status_code']
To access an object property that has funky characters in the name, quote the name and stick it in braces.
print $data->{'#http_status_code'};
Or, say $data = json_decode($json_entry, true); to get the data back as an array.
PHP cwill give syntax error when you do this:
$data->#http_status_code;
it looks for $http_status_code variable which is not present
so in order to make this work you have to do this:
echo $data->{'#http_status_code'};
Try this:
$data = json_decode($json_entry, true);
print $data["#http_status_code"];