After a http post in c# to php am getting an output of the form
in the code i have
public function actionSubmitInspection(){
$data = $_POST;
return (array)$data["check_comments"];
}
now am getting a result of the form
[
"[{\"id\":26,\"comment\":\"89oiu\"},{\"id\":27,\"comment\":\"comment 2\"}]"
]
as from my try typecasting array doesnt creating the array, How can i convert the serialized string to an array or an object.
use json_decode function.
public function actionSubmitInspection(){
$data = $_POST;
// replace it
//return (array)$data["check_comments"];
return json_decode($data["check_comments"]);
}
Out put will be array of objects.
Array
(
[0] => stdClass Object
(
[id] => 26
[comment] => 89oiu
)
[1] => stdClass Object
(
[id] => 27
[comment] => comment 2
)
)
as from my try typecasting array doesnt creating the array
Yes, it creates an array but the array it creates contains the JSON text.
You need to parse the JSON in order to restore the data structures it encodes. PHP provides the function json_decode() for this purpose. I recommend you pass TRUE as the second argument to json_decode() to get back arrays (otherwise it creates stdClass objects that are just arrays with a fancy syntax and limited options for processing).
// Assuming the value of $data['check_comments'] is:
// "[{\"id\":26,\"comment\":\"89oiu\"},{\"id\":27,\"comment\":\"comment 2\"}]"
$output = json_decode($data['check_comments']);
print_r($output);
The output:
Array
(
[0] => Array
(
[id] => 26
[comment] => 89oiu
)
[1] => Array
(
[id] => 27
[comment] => comment 2
)
)
You should use json_decode($data["check_comments"]) the output will be an array of stdClass objects:
Array
(
[0] => stdClass Object
(
[id] => 26
[comment] => 89oiu
)
[1] => stdClass Object
(
[id] => 27
[comment] => comment 2
)
)
or passing true on second param, json_decode($data["check_comments"], true) and the output will be an array of arrays:
Array
(
[0] => Array
(
[id] => 26
[comment] => 89oiu
)
[1] => Array
(
[id] => 27
[comment] => comment 2
)
)
Related
how get value feild[id] in php code
stdClass Object ( [List_inserted] => Array ( [0] => stdClass Object ( [ID] => 145001 [value] => 40 ) ) [Sucssess] => 1 [ErrorMassage] => OK )
You didn't give us a name of stdClass so I'm assuming it's $stdClass.
$stdClass->List_inserted[0]->ID
Let's break it down;
stdClass Object ( [List_inserted] => Array ( [0] => stdClass Object ( [ID] => 145001 [value] => 40 ) ) [Sucssess] => 1 [ErrorMassage] => OK )
We access objects with a -> and we access arrays with []
The first part tells us it's an object, so it's;
$stdClass->List_inserted
List_inserted is an array thanks to the => Array. We can access this with [0].
$stdClass->List_inserted[0]
Well, List_inserted[0] is an object, thanks too [0] => stdClass Object; and you wanted to access the ID? So we need another ->
$stdClass->List_inserted[0]->ID
I trying to get a array in VB.NET, but I have troubles for deserialize, I don't know if my format is bad or what, but first the data is a std object
Array
(
[0] => stdClass Object
(
[id] => 6797892
[marca] => xxx
[details] => yyy
[price] => rrr
[info] => Array
(
[0] => stdClass Object
(
[Items] => Array
(
[0] => stdClass Object
(
[Por] => 1
[$$hashKey] => 03F
)
)
[Tipo] => mouse
[price] => 1.65
[$$hashKey] => 03D
)
[1] => stdClass Object
(
[Items] => Array
(
[0] => stdClass Object
(
[Por] => o
[$$hashKey] => 03J
)
)
[Tipo] => teclado
[price] => 1.65
[$$hashKey] => 03H
)
)
[$$hashKey] => 03B
)
)
next i use json_encode(in php):result is:
[{"Id":"6797904","marca":"xxx","Pais":"yyy","Liga":"rrr","Jornada":"3","info":[{"Items":[{"Por":"1","Cuota":"2.25","$$hashKey":"03I"}],"Tipo":"mouse","price":2.25,"$$hashKey":"03G"}],"$$hashKey":"03E"}]
and using
....
file_put_contents($file, print_r($current, true) );
...
I save it in items.txt, and I load it in vb.net, but I don't know what is the correct way to convert into an array using:
Dim str As JArray = JArray.Parse(TextBox1.Text)
Dim results As Object = str("Id").ToString
To put in it an answer format.
The use of print_r is not correct here.
...
file_put_contents($file, print_r($current, true) );
...
The definition for print_r as per php.net:
print_r — Prints human-readable information about a variable.
This means extra characters are added to make it readable to humans, but not for machines. It even creates invalid JSON, causing VB.NET to generate an error.
Update your code to
...
file_put_contents($file,$current);
...
And it should work
I have a JSON array that I want to be able to drill down to a lower level and print just that value. The problem occurs when I reach a level that has is indacted as [0] (or [n]). For example I have the following output, and I want to just print the game key for the first league.
This is how I am trying to print it
HtmlSpecialChars(print_r($user->fantasy_content->users[0]->user[1]->games[0]->game[0]->game_key,1))
However I keep getting this error:
Cannot use object of type stdClass as array
When I do it incrementally it seems to fail on this command (so I assume I'm not index correctly):
$user->fantasy_content->users[0]
Here is the output:
stdClass Object
(
[fantasy_content] => stdClass Object
(
[xml:lang] => en-US
[yahoo:uri] => /fantasy/v2/users;use_login=1/games
[users] => stdClass Object
(
[0] => stdClass Object
(
[user] => Array
(
[0] => stdClass Object
(
[guid] => IYEZUHTVBYRLIB3OAQC5WRZPQY
)
[1] => stdClass Object
(
[games] => stdClass Object
(
[0] => stdClass Object
(
[game] => Array
(
[0] => stdClass Object
(
[game_key] => 147
[game_id] => 147
[name] => Baseball
[code] => mlb
[type] => full
[url] => http://baseball.fantasysports.yahoo.com/b1
[season] => 2006
)
)
)
[count] => 1
)
)
)
)
[count] => 1
)
[time] => 52.390813827515ms
[copyright] => Data provided by Yahoo! and STATS, LLC
[refresh_rate] => 60
)
)
For objects you must use the -> syntax and if the key/property name is a number or has other special characters, you will need to use the $object->{'0'} syntax.
The game_key can be retrieved using:
$user->fantasy_content->users->{'0'}->user[1]->games->{'0'}->game[0]->game_key;
You can convert a stdClass object to an array by casting it like so:
<?php
$array = (array) $myObject;
echo json_encode($array);
You can also cast inline:
<?php
echo json_encode((array) $object);
I tried to get an answer for this in other posts with no luck, hope someone can help me here, i have a multidimensional array:
Array (
[0] => stdClass Object (
[affectsVersions] => Array ( )
[assignee] => hmontes
[attachmentNames] => Array ( )
[components] => Array ( )
[created] => 2012-08-15T05:31:26.000Z
[customFieldValues] => Array (
[0] => stdClass Object (
[customfieldId] => customfield_10201
[key] => [values] => Array (
[0] => 123456
)
)
[1] => stdClass Object (
[customfieldId] => customfield_10004
[key] => [values] => Array (
[0] => 30
)
)
)
[description] => [duedate] => [environment] => [fixVersions] => Array ( )
[id] => 10228
[key] => NTP-29
[priority] => 3
[project] => NTP
[reporter] => hmontes
[resolution] => [status] => 1
[summary] => case 123456
[type] => 3
[updated] => 2012-08-15T05:31:26.000Z
[votes] => 0
)
)
this is what i get when i do a print_r with the array variable, i need to search and get the value from [key] that would be in this case NTP-29 and keep it in a variable as string.
You can get the value of an array by the key using $array['keyName'];
But, for you it looks like you just need to go deeper $array[0]['key'];
Both arrays values and properties of objects can be accessed using associative array syntax. To get the value of the key property in your object within the array you'd do the following, assuming $array is a variable containing a reference to your array:
$key = $array[0]['key']; // accesses NTP-29 in this case.
Here's another way to access the same property, using object property-access syntax:
$key = $array[0]->key; // also accesses NTP-29.
I have got this result using json decode from an api call. but I dont know how to extract "VALUE" from this result..
$obj=json_decode($json_string);
print_r($obj);
stdClass Object ( [status] => OK [data] => stdClass Object ( [trends] => stdClass Object ( [rank] => Array ( [0] => stdClass Object ( [date] => 201011 [value] => 7196 ) ) ) [trends_low_sample] => [query_cost] => 1 [trends_frequency] => monthly ) )
I need only "7196" from this result. how do I do this??
Ah! Based on your updated code you're tring to get the value from PHP not Javascript? Personally I use json_decode($json_string,true); to get an associative array (json_decode), if you do that it should be accessible as:
echo $obj["data"]["trends"]["rank"][0]["value"];
As an object it's accessible as:
echo $obj->data->trends->rank[0]->value;