When I try to json encode an array using static value it output like :
[
{"data":[0,0,0,0,0,5],"name":"www.google.com"},
{"data":[0,0,0,0,0,4],"name":"www.yahoo.com"},
{"data":[0,0,0,0,85,0],"name":"www.bing.com"}
]
then I tried json encode using dynamic value it output like this
[
{"data":[0,0,0,0,0,"5"],"name":"www.google.com"},
{"data":[0,0,0,0,0,"4"],"name":"www.yahoo.com"},
{"data":[0,0,0,0,"85",0],"name":"www.bing.com"}
]
for non-zero value, there a extra double quotes ("") how remove it ?
You need parse with intval, example:
$arr = array("1", intval("2"));
echo json_encode($arr);
Outputs:
["1",2]
Related
I have an array in string given below.
$string="
"Status":true,
"ReVerifiedCount":1,
"ProfilePrefix":"INVTRK"
";
How I can get array from this string same as it exists in string.
<?php
$string='{
"Status":true,
"ReVerifiedCount":1,
"ProfilePrefix":"INVTRK"
}';
$data=json_decode($string,true);
print_r($data);
I formated your string-json the right way. Your double quotes and the missing brackets were creating the main problem as your input was not a valid json.
Output is this:
Array ( [Status] => 1 [ReVerifiedCount] => 1 [ProfilePrefix] => INVTRK )
First your String looks like a json string.
$string='{
"Status":true,
"ReVerifiedCount":1,
"ProfilePrefix":"INVTRK"
}';
This is the correct form.
To parse it use json_decode from PHP
$parsedArray = json_decode($string, true);
Here is a link to the doc : http://php.net/manual/en/function.json-decode.php
I'm having troubles with json_decode in PHP:
I have this on file:
{1: ['oi','oi'], 2: ['foo','bar']}
And this is my php code:
<?php
$string = file_get_contents("quizen.json"); // the file
$json = json_decode($string);
echo $json[1][0]
?>
But the echo returns anything, I used var_dump, and I get NULL!
What's the problem?
The issue is that your file is not valid JSON since it uses single quotes for strings and has integers as object keys:
{1: ['oi','oi'], 2: ['foo','bar']}
Also, since the JSON is an object, you should decode it to an associative array using json_decode($string, true).
According to the JSON spec:
A value can be a string in double quotes, or a number, or true or false or null, or an object or an array.
Also, the object keys need to be strings.
If you change the single quotes to double quotes and edit your PHP's decode_json call to decode to an associative array, it should work. For example:
JSON:
{"1": ["oi","oi"], "2": ["foo","bar"]}
PHP:
<?php
$string = file_get_contents("quizen.json"); // the file
$json = json_decode($string, true); // set to true for associative array
echo $json["1"][0];
?>
Is there anything that i print the string without the array information?
(Without this: a:1:{i:0;s:4011:" ";} )?
The whole array must be in single variable and printed. Foreach doesn't help, neither print(array[0]).
You can use json_encode() which will give you less verbose string representation:
<?php
$a = array('a', 'bb' => 'ccc');
echo json_encode($a);
// outputs {"0":"a","bb":"ccc"}
You can use
echo json_encode($array);
This will print a json encoded string.
For you to decode it you can use
json_decode(json encoded string);
You can see docs of php about json encode and json decode
How can I parse json array string to array on php
'[{"a": "1", "b": "2"}, {"a": "3"}]'
seems json_decode allows parse only objects but not arrays. Should it be manually parsed to array before using json_decode?
Seems problem in string. I get a variable with json, and if I output it, looks like the json is valid
echo($jsonvar); //result [{"title":"Home","id":"/","url":"/"}]
but when I try parse string from the variable, the result is nothing even when string is trimmed
echo('[{"title":"Home","id":"/","url":"/"}]', true); //nice parsed array
echo($jsonvar, true); //nothing
echo(trim($jsonvar, " \t\n\r\0\x0B"), true); //nothing
Pass the true as a second parameter to your json_decode() to parse the json string to array.
$json='[{"a": "1", "b": "2"}, {"a": "3"}]';
$arr= json_decode($json,true);
print_r($arr);
You can get the json into array by using the true flag in json_decode()
<?php
$str = '[{"a": "1", "b": "2"}, {"a": "3"}]';
$arr=json_decode($str,true);
print_r($arr);
I need to grab a json-string from this page: https://retracted.com
If you view the source, I json-string starts after var mycarousel_itemList =. I need to parse this string as a correct json-array in my php-script.
How can this be done?
EDIT: I've managed to pull this off using explode, but the method is ugly as heck. Is there no build-in function to translate this json-string to a array?
To clarify: I want the string I grab (which is correct json) to be converted into a php-array.
The JSON in the script block is invalid and needs to be massaged a bit before it can be used in PHP's native json_decode function. Assuming you have already extracted the JSON string from the markup (make sure you exclude the semicolon at the end):
$json = <<< JSON
[ { address: 'Arnegårdsveien 32', … } ]
JSON;
var_dump(
json_decode(
str_replace(
array(
'address:',
'thumb:',
'description:',
'price:',
'id:',
'size:',
'url:',
'\''
),
array(
'"address":',
'"thumb":',
'"description":',
'"price":',
'"id":',
'"size":',
'"url":',
'"'
),
$json
)
,
true
)
);
This will then give an array of arrays of the JSON data (demo).
In other words, the properties have to be double quoted and the values need to be in double quotes as well. If you want an array of stdClass objects instead for the "{}" parts, remove the true.
You can do this either with str_replace as shown above or with a regular expression:
preg_match('
(.+var mycarousel_itemList = ([\[].+);.+function?)smU',
file_get_contents('http://bolig…'),
$match
);
$json = preg_replace(
array('( ([a-z]+)\:)sm', '((\'))'),
array('"$1":', '"'),
$match[1]
);
var_dump(json_decode($json, true));
The above code will fetch the URL, extract the JSON, fix it and convert to PHP (demo).
Once you have your json data, you can use json_decode (PHP >= 5.2) to convert it into a PHP object or array