I am making a web app. In one part of it, I have JS send a JSON string to PHP. The conent of the string is:
{"date":"24-03-2014","Cars":["Cheap","Expensive"]}
I want to convert the string into an object, for which I am doing:
$meta = $_POST["meta"];
$obj = json_decode($meta);
echo $obj->date;
Anyhow, Instead of having 24-03-2014 as the output, I am getting a blank line as the output.
What's wrong? What's the correct way of doing this?
Not able to re-produce it:
$jsonStr = '{"date":"24-03-2014","Cars":["Cheap","Expensive"]}';
$jsonObj = json_decode($jsonStr);
var_dump($jsonObj);
var_dump($jsonObj->date);
Outputs:
object(stdClass)[1]
public 'date' => string '24-03-2014' (length=10)
public 'Cars' =>
array (size=2)
0 => string 'Cheap' (length=5)
1 => string 'Expensive' (length=9)
string '24-03-2014' (length=10)
Are you sure your $_POST['meta'] is set & has values?
Below works like a charm. Your $_POST["date"] has not correct value inside. Try var_dump($_POST) to debug it.
<?php
$input = '{"date":"24-03-2014","Cars":["Cheap","Expensive"]}';
$meta = $input;
$obj = json_decode($meta);
var_dump($obj->date); //Prints string(10) "24-03-2014"
?>
Related
I have this script to display some value from a json file:
<?php
//read the json file contents
$jsondata = file_get_contents('http://website.com/file.json');
//convert json object to php associative array
$data = json_decode($jsondata, true);
//get the weather details
$icon = $data['weather']['icon'];
//Display variables
echo "icon value: $icon"; ?>
the json file is :
{"coord":{"lon":130.84,"lat":-12.46},"weather":[{"id":501,"main":"Rain","description":"moderate rain","icon":"10n"}],"base":"stations","main":{"temp":21,"pressure":1011,"humidity":100,"temp_min":21,"temp_max":21},"visibility":5000,"wind":{"speed":1.5,"deg":130},"clouds":{"all":90},"dt":1479589200,"sys":{"type":1,"id":8209,"message":0.162,"country":"AU","sunrise":1479501624,"sunset":1479547451},"id":2073124,"name":"Darwin","cod":200}
I need to display the icon value (10n), but my script didn't work... Brackets in weather section give me some trouble...
thanks for your help
you should access this way
$icon = $data['weather'][0]['icon'];
//Display variables
echo "icon value: $icon"; ?>
output
icon value: 10n
var_dump will help you to understand structure of array.this is how $data['weather'] looks like.
//var_dump($data['weather']);
array (size=1)
0 => // you forget to access this element first?
array (size=4)
'id' => int 501
'main' => string 'Rain' (length=4)
'description' => string 'moderate rain' (length=13)
'icon' => string '10n' (length=3)
weather is a array.so you should access nth element first.in this case first index $data['weather'][0] then you can access icon
I have a problem I am getting a json file I can echo it and it looks like this
[{"sys_data":"0MxPPaza","date":"2015-02-15","objective":"VIDEO"}]
in my code I am doing this
$json = FROM THE SERVER;
$obj = json_decode($json);
$res = $obj->["objective"];
echo $res;
res is NULL obj is NULL also
Your json_decode call returns an array, with one member.
Here is a dump of your json object:
array (size=1)
0 =>
object(stdClass)[10]
public 'sys_data' => string '0MxPPaza' (length=8)
public 'date' => string '2015-02-15' (length=10)
public 'objective' => string 'VIDEO' (length=5)
so replace this line:
$res = $obj->["objective"];
With this:
$res = $obj[0]->objective;
Just replace [] quotes to {}. Like $res = $obj[0]->{"objective"};
Or you may use assoc array convertation instead of object:
$json = FROM THE SERVER;
$obj = json_decode($json, true);
$res = $obj["objective"];
echo $res;`
Following code:
$names = file_get_contents('names.txt');
var_dump($names);
Is returning in var_dump:
array (size=1)
0 => string 'asd
dwqd
asfa
fewfw' (length=22)
Meanwhile, this returns correct way:
$test = array('tete','asdasd','yryr');
var_dump($test);
Result:
array (size=3)
0 => string 'tete' (length=4)
1 => string 'asdasd' (length=6)
2 => string 'yryr' (length=4)
So, the code block when I try to get names from file and make an array of them return an wrong format array. names.txt is built as following:
asd
dwqd
asfa
fewfw
Your problem is you're not splitting the lines into an array, it's just reading the whole files as a block of text.
Use file() with the FILE_IGNORE_NEW_LINES flag. The flag says Do not add newline at the end of each array element, turning your multiple lines into single lines of an array.
Try:
$names = file('names.txt', FILE_IGNORE_NEW_LINES);
var_dump($names);
You are just printing normal string from text file. You need to explode in to array.
Use,
$array = explode(" ",$names);
var_dump($array);
Use file():
$names = trim(file('names.txt'));
var_dump($names);
I'm looking for a way to access this JSON file data. What I'm interested in is to extract the property named documentjsonblob, as below.
object(Quadrem\Model\Order)[201]
protected '_dateCreated' => null
protected '_buyerCode' => null
array (size=2)
2415 =>
array (size=23)
'#storeid' => string '813' (length=3)
'#active' => string '1' (length=1)
'#created' => string '2013-11-25 12:28:21' (length=19)
'documentjsonblob' => string '{"HEAD":{"ORDERSEQUENCE":"0","TOTAL_TAX":7.9,"TOTAL_AMOUNT":86.9,"NUMBER":"AKMon3","TYPE":"NB","TYPE_NAME":"Standard PO","SUPPLIER":"0000002122","CREATED":"2013-04-29T12:00:00Z","CONTRACT_NUMBER":"","EXTERNAL_REFERENCE":"","CONTACT_PERSON":"Caroline Howlett","COMMENT":[""],"CURRENCY":"AUD","DELIVERY_TERMS":"DeliveryCondition|","PAYMENT_TERMS":[{"TEXT1":"21st day of next month after receipt"}],"NET_VALUE":79.0,"DELIVERY_ADDRESS":{"NAME_1":"KGTP FACILITY","NAME_2":"GAS TREATMENT PLANT","NAME_3":"KGPF","POSTAL_CODE":"6714","CITY":"Karratha","STREET":"Withnell Bay","REGION":"AUWA","REGION_NAME":"AUWA","COUNTRY":"AU"}]}' (length=3029)
'documenttype' => string 'PO' (length=2)
1890 =>
array (size=23)
'#storeid' => null
'#active' => null
I'm not quite sure which kind of an encoded JSON file this is by the way it looks. Would appreciate it if somebody could help.
Thanks.
Since in the comment you have mentioned it is the out put of var_dump and you want to get documentjsonblob, let's follow this:
On first line you have object(Quadrem\Model\Order)[201], that means you var_dumped an object, say $my_obj
On line four array (size=2) says you have an array, and what you need is located in the first element of the array, so $my_obj[0]
$my_obj[0] holds an associative array, and the desired index is documentjsonblob, so we can get the String representation as:
$my_arr = $my_obj[0];
$json_str = $my_arr['documentjsonblob'];
Now $json_str holds the String representation, to convert it to an PHP object:
$json_obj = json_decode($json_str);
So now $json_obj holds the object you want.
I have some very odd issue,
I'm trying to parse some json string with php, string is in array, and if I use simple
json_decode($my_array[0],true)
it doesn't work, but, if I just copy a string from var_dump($my_array) and try to decode it works 100% ok.
Any ideas what could be wrong ?
Json string:
{"mode":"view","pid":"243","documentId":"193"}
Kuba,
Here's the syntax for converting a json array into a php associative array:
$my_array = '{"mode":"view","pid":"243","documentId":"193"}';
$new_array = json_decode($my_array,true);
echo $new_array['mode']; //return: view
echo $new_array['pid']; //return: 243
echo $new_array['documentId']; //return: 193
var_dump() result:
array (size=3)
'mode' => string 'view' (length=4)
'pid' => string '243' (length=3)
'documentId' => string '193' (length=3)
Ok, I've found the solution, after data serialization I've encoded string using base64_encode and then push It in that form to other scripts.
I still don't know why I had to encode it with base64, maybe because this string is used in javascript scripts and after that decoded in php?