I'm sending this JSON:
[{"tipo":""},{"activo":""},{"titulo":"Servicoasd B"},{"texto":"asdasdasd"}]
to a php file via post method.
There, i do
$obj = json_decode($_POST['sentJson']);
However, I seem to be unable to access the elements of the JSON.
var_dump(($obj));
Shows the object:
array(4) {
[0]=>
object(stdClass)#2 (1) {
["tipo"]=>
string(0) ""
}
[1]=>
object(stdClass)#3 (1) {
["activo"]=>
string(0) ""
}
[2]=>
object(stdClass)#4 (1) {
["titulo"]=>
string(9) "Servico B"
}
[3]=>
object(stdClass)#5 (1) {
["texto"]=>
string(6) "asdasd"
}
}
But if I try
$obj['texto'];
$obj->{'texto'};
$obj[0]['texto'];
$obj[0];
It shows "undefined index texto" or "trying to get property of non object in" and the last one "Object of class stdClass could not be converted to string in". I'm very new to PHP, but still I can't seem to notice what I'm doing wrong. Any help would be appreciated.
Your JSON is a serialized array of four completely different objects, so when you run json_decode, that's what you get: an array.
If you want to access your objects inside that array, access them like you would any other indexed array:
$list = json_decode(...);
foreach($list as $obj) {
var_dump($obj)
}
Or target them explicitly using plain old numerical access.
$list = json_decode(...);
$last = $list[3];
$text = $last->texto;
But really the question you should be asking is why this is the JSON you get. An array with completely different objects at each position is terrible data, and should be fixed.
Related
I have an api wrapper i am using that returns something like this
object(stdClass)#7 (1) {
[0]=>
object(stdClass)#6 (2) {
["contactId"]=>
string(2) "nV"
["email"]=>
string(31) "email#domain.com"
}
}
how do i access the email part with PHP
Cast your API returned data to an array.
For example you are saving API returned data in $result variable. Cast it to an array.
$arrayResult = (array) $result;
echo $arrayResult[0]->email;
Try this.
I have a simple answer I just can't solution to:
var_dump(obj) =
object(stdClass)#15 (3) {
["properties"]=>
object(stdClass)#14 (2) {
["user_name"]=>
string(4) "somename"
["email_address"]=>
string(12) "test#test.com"
["arrays"]=>
object(stdClass)#17 (1) {
["sites[]"]=>
object(stdClass)#18 (4) {
["0"]=>
int(1)
["1"]=>
int(1)
["2"]=>
int(0)
["3"]=>
int(0)
}
}
}
How to call the 'sites[]' object in my 'obj'?
I tried the following:
obj->sites[]
obj->{'sites[]'}
Both options aren't working...
It might be better to clean up the code that generates that object, but you should be able to access the sites[] object via:
$sites = $obj->arrays->{'sites[]'};
However $sites will still be an object, so you would need to access its elements in a similarly awkward way:
echo $sites->{'0'};
It would be better to cast it to an array at that point:
$sites = (array) $obj->arrays->{'sites[]'};
Then you can access as an array:
echo $sites[0];
EDIT, seams you cannot access array elements indexed by a numerical string.
A better option (as discovered by a SO question i just posted about this) would be to use get_object_vars:
$sites = get_object_vars($obj->arrays->{'sites[]'});
Then you can access as an array:
echo $sites[0];
I'm trying to extract a value from a JSON string that's in my program. The output of var_dump($obj); is this:
object(stdClass)#1 (1) {
["BTC"]=>
object(stdClass)#2 (2) {
["value"]=>
float(403.645)
["currency"]=>
string(3) "GBP"
}
}
What I want to access is the value (currently 403.645 in this example), but I can't work out how to do it.
I've tried $obj->value and other combinations, but get nowhere; it appears to me that this is an object inside an object and as a result I can't find out how to access it. Any help would be appreciated!
i passed a json_encoded array to javascript. Now i would like to acces that array to get the different elemtnts.
i print it out in the console.log() and i get this array:
array(1) {
[16]=>
array(2) {
[3488]=>
array(1) {
[0]=>
array(2) {
["article_no_internal"]=>
string(6) "999184"
["article_name_internal"]=>
string(29) "Geschenkbox Kerzenschein 2011"
}
}
[2615]=>
array(1) {
[0]=>
array(2) {
["article_no_internal"]=>
string(6) "700469"
["article_name_internal"]=>
string(29) "Hotelscheck RomantischeTagef2"
}
}
}
}
This is about right. How can i access the article_name of the second array, with the ID 2615?
found a related question here reading a jsone object, hope for some better explebation or answer. Thanks.
EDIT:
As it seems i made a mistake, i showed a php var_dump in the console. When i try to show the javascript array in the console i get undefined.
Since JSON means "JavaScript Object Notation" you don't need to do anything to access the object's items.
For example you can access:
jsonObject[2615][0]["article_name_internal"]
if this object is String, use eval to convert the string to a JavaScript object and access the items in the same way with the previous example.
var jsonObject = eval(jsonstring);
jsonObject[2615][0]["article_name_internal"]
My question: How can I break up and iterate through the JSON array pictured below?
I am making an AJAX web app and I need to serialize an array of objects in Javascript and put them in a url to pass to a php script. This is all going fine and the php script recieves the JSON like so..
$passed = $_GET['result'];
if(isset($passed)){
$passed = str_replace("undefined" , " " , $passed); /*had to add this to remove the undefined value*/
$json = json_decode(stripslashes($passed));
echo"<br/>";
var_dump($json ); //this is working and dumps an array
}
When I call var_dump on the decoded JSON I echo an output like so...
array(1) { [0]=> object(stdClass)#70 (2) { ["itemCount"]=> int(0) ["ItemArray"]=> array(2) { [0]=> object(stdClass)#86 (6) { ["itemPosition"]=> int(0) ["planPosition"]=> int(0) ["Name"]=> string(5) "dsfsd" ["Description"]=> string(3) "sdf" ["Price"]=> string(0) "" ["Unit"]=> string(0) "" } [1]=> object(stdClass)#85 (6) { ["itemPosition"]=> int(1) ["planPosition"]=> int(0) ["Name"]=> string(4) "fdad" ["Description"]=> string(3) "sdf" ["Price"]=> string(0) "" ["Unit"]=> string(0) "" } } } }
The JSON
This is the JSON I am receiving. It seems like some of the pairs don't have names? How can I access elements in this Array?
Thanks alot guys
Some of these elements are coming back as stdClass objects as you can see in the var_dump output. You can get at the attributes with the standard object notation, for example, with your $json variable:
echo $json[0]->itemCount; // 0
echo $json[0]->itemArray[0]->itemPostion; // 0
You can also iterate over stdClass instances just like any PHP object, you'll be looping through the public data members, so again with your $json:
foreach(echo $json[0]->itemArray[0] as $key => $value)
echo 'key: ' . $key . ', value: ' . $value . PHP_EOL;
will loop through that first object, echoing out the member names and values of the object.
You just access them by index:
data[0] // first data item
Note that that's how you would "normally" access an array in the usual sense, so I might be missing something about your question here...