numeric Json decode not work - php

i have below json code
{"1":1,"5":1}
when i decode the above json i got object array using the below php statement.
$array_val = (array)json_decode($price);
i got a below array.
Array
(
[1] => 1
[5] => 1
)
but the below statement does not work
echo $array_val[1];
the below error occurred.
Undefined offset: 1
How to resolve this issue?

try this DEMO
PHP
$json = '{"1":1,"5":1}';
$array_val=json_decode($price, true);
echo $array_val[1];
OUTPUT :
1

Note that json_decode($string) returns an object, not an array (which is why your code doesn't behave).
To return an array instead, use:
$arr = json_decode($string, true);
See also http://php.net/manual/en/function.json-decode.php

You can get this using below code
$array_val=json_decode($price);
echo $array_val->{1}
OR
$array_val=json_decode($price,true);
echo $array_val[1]

Related

Error when trying to echo an 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);

How to display Json Data in PHP?

I have following JSON Data and I tried to display Json data using PHP but it did not display result. Here is my JSON Data
$ads={"ReferenceNo":"KWPOG0QoXU","Message":"SUCCESS","Status":1,"ResponseStatus":200}
I tried following code but it did not work.
$ads1 = json_decode($ads);
echo $ads1->ReferenceNo;
Kindly help me how to display result in PHP. Thanks in advance.
You should parse data as string & then do like this:
$json = '{"ReferenceNo":"KWPOG0QoXU","Message":"SUCCESS","Status":1,"ResponseStatus":200}';
$array = json_decode($json, true);
echo '<pre>'; print_r($array);
Output:
Array
(
[ReferenceNo] => KWPOG0QoXU
[Message] => SUCCESS
[Status] => 1
[ResponseStatus] => 200
)
To get Data, Code like this:
echo $array['ReferenceNo'];
Let me know for further help.
For people interested in pulling data from a location, instead of adding the json on the file, use the following;
$json = file_get_contents('MYLOCATION.json');
$array = json_decode($json, true);
echo '<pre>'; print_r($array);
happy coding :)

PHP json_decode return empty array

I just test this sample from php doc (http://au2.php.net/manual/en/function.json-decode.php)
here is my code:
<?php $json = '{"a":1,"b":2,"c":3,"d":4,"e":5}'; echo json_decode($json, true), '<br />';?>
But it just returns an EMPTY array.
Have no idea why...Been searching around but no solution found.
PLEASE help!
You can validate at following
website: http://jsonlint.com/
You have to use a php "json_decode()" function to decode a json encoded data.
Basically json_decode() function converts JSON data to a PHP array.
Syntax: json_decode( data, dataTypeBoolean, depth, options )
data : - The json data that you want to decode in PHP.
dataTypeBoolean(Optional) :- boolean that makes the function return a PHP Associative Array if set to "true", or return a PHP stdClass object if you omit this parameter or set it to "false". Both data types can be accessed like an array and use array based PHP loops for parsing.
depth :- Optional recursion limit. Use an integer as the value for this parameter.
options :- Optional JSON_BIGINT_AS_STRING parameter.
Now Comes to your Code
$json_string = '{"a":1,"b":2,"c":3,"d":4,"e":5}' ;
Assign a valid json data to a variable $json_string within single quot's ('') as
json string already have double quots.
// here i am decoding a json string by using a php 'json_decode' function, as mentioned above & passing a true parameter to get a PHP associative array otherwise it will bydefault return a PHP std class objecy array.
$json_decoded_data = json_decode($json_string, true);
// just can check here your encoded array data.
// echo '<pre>';
// print_r($json_decoded_data);
// loop to extract data from an array
foreach ($json_decoded_data as $key => $value) {
echo "$key | $value <br/>";
}
you should not use echo because it is an array. use print_r or var_dump .it works fine
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
print_r(json_decode($json, true));
Output:
Array
(
[a] => 1
[b] => 2
[c] => 3
[d] => 4
[e] => 5
)
No, it doesn't return an empty array.
Printing an array with echo just prints a string "Array()".
Use print_r or var_dump to get the structure of the variable.
In newer PHP it will also emit a notice when using echo on an array ("Array to string conversion"), so you shouldn't do it anyway. The manual you've mentioned changed to print_r.
It works fine, but you use wrong method to display array.
To display array you cannot use echo but you need to use var_dump
It works fine as others mention, but when you print the array it is converted to string, which means only the string "Array" will be printed instead of the real array data. You should use print_r(), var_dump(), var_export() or something similar to debug arrays like this.
If you turn on notices you will see:
PHP Notice: Array to string conversion in ...
The example you linked uses also var_dump for the same reason.
var_dump have pretty print in php5.4
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
var_dump( json_decode($json));

unable to decode json variable

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);

JSON decode with PHP: Key started with # sign

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"];

Categories