deseralize php Json in vb.net - php

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

Related

Parsing JSON Array Data

I'm trying to get data out of some JSON DATA. I'm using the following lines to decode it right now
$json_array = (array)(json_decode($response));
When I print my JSON Decoded array, I have the following data below:
I would like to get the details from the details section, where there is multiple sets of from/to_date's, and up/down numbers. Nothing I seem to do works though to get me to that data. I can print out the data from other areas like usage, but, I can't get into the details.
Array (
[resp_code] => SUCCESS
[caller_ref] => 2017092002282130006180
[server_ref] => 2017092002282169760291
[data] => stdClass Object (
[type] => monthly
[group_id] => 19
[device_id] => 32
[sn] => sn1234
[usages] => Array (
[0] => stdClass Object (
[from_date] => 2017-09-01T00:00:00
[to_date] => 2017-09-30T23:59:59
[up] => 22370
[down] => 119217
[ts] => 2017-09-01T00:00:00
)
)
[details] => stdClass Object (
[3] => Array (
[0] => stdClass Object (
[from_date] => 2017-09-01T00:00:00
[to_date] => 2017-09-30T23:59:59
[up] => 5522
[down] => 40301
[ts] => 2017-09-01T00:00:00
)
)
[2] => Array (
[0] => stdClass Object (
[from_date] => 2017-09-01T00:00:00
[to_date] => 2017-09-30T23:59:59
[up] => 6905
[down] => 32029
[ts] => 2017-09-01T00:00:00
)
)
)
)
)
Whats wrong with objects?
$obj = json_decode($response);
echo $obj->data->details[0]->from_date;
Or to loop it:
foreach ($obj->data->details as $item) {
echo $item->from_date;
// same goes for: to_date, up etc.
}
Simple and sexy!
Update:
It looks as if $item would be an array as well, so if you have problems try:
foreach ($obj->data->details as $item) {
echo $item[0]->from_date;
// same goes for: to_date, up etc.
}

PHP "Illegal string offset" error when looping through array

I am using the Amazon API to get some XML data, which is then passed through the json_decode() function.
Here are two samples of the data that is returned:
[BrowseNodes] => Array
(
[Request] => Array
(
[IsValid] => True
[BrowseNodeLookupRequest] => Array
(
[BrowseNodeId] => 2645269011
[ResponseGroup] => BrowseNodeInfo
)
)
[BrowseNode] => Array
(
[BrowseNodeId] => 2645269011
[Name] => Featured Categories
[Children] => Array
(
[BrowseNode] => Array
(
[0] => Array
(
[BrowseNodeId] => 3741261
[Name] => Cooktops
)
[1] => Array
(
[BrowseNodeId] => 3741271
[Name] => Dishwashers
)
[2] => Array
(
[BrowseNodeId] => 3741331
[Name] => Freezers
)
[3] => Array
(
[BrowseNodeId] => 2399939011
[Name] => Ice Makers
)
)
)
and
[BrowseNodes] => Array
(
[Request] => Array
(
[IsValid] => True
[BrowseNodeLookupRequest] => Array
(
[BrowseNodeId] => 3774781
[ResponseGroup] => BrowseNodeInfo
)
)
[BrowseNode] => Array
(
[BrowseNodeId] => 3774781
[Name] => Vitamin D
[Children] => Array
(
[BrowseNode] => Array
(
[BrowseNodeId] => 6936848011
[Name] => D3
)
)
I am then using this code to obtain data for each of the children:
if(isset($result['BrowseNodes']['BrowseNode']['Children'])){
$childs = $result['BrowseNodes']['BrowseNode']['Children']['BrowseNode'];
foreach($childs as $child){
$browsenodeid = $child['BrowseNodeId'];
$name = $child['Name'];
}
}
Everything works fine in the first sample, but in the second one, I am getting the following errors:
Warning: Illegal string offset 'BrowseNodeId'
Warning: Illegal string offset 'Name'
If I echo $browsenodeid and $name in the second example, it gives me 6 and D as the output.
Any ideas as to what I am doing wrong? I am quite confused; to me the data I am looping through in both cases seems to be the same, the only difference being that in the first case, the array has 4 elements and in the second it has 1 element.
Thanks in advance.
The reason I think is that in case 1: the structure is [BrowseNode][0][BrowseNodeId] whereas in case 2 it is : [BrowseNode][BrowseNodeId] that [0] is missing , you can fix it in the data.
Read more at: Illegal string offset Warning PHP

How can I merge or search an object?

Here's my issue:
I have an object filled with arrays that look like this.
[376339] => Array
(
[0] => 1f422730-f54b-4e4d-9289-10258ce74446
[1] => 60dc4646-06ce-44d0-abe9-ee371847f4df
)
I need to search another object to find objects with the matching IDs, like below. Is there a way of doing this without a foreach? There are SEVERAL and I would like to not have to loop over the entire object every time.
stdClass Object
(
[id] => 1f422730-f54b-4e4d-9289-10258ce74446
[percentage] => 32
[destinations] => Array
(
[0] => stdClass Object
(
[id] => 59826
[destination_id] => 59826
[type] => Destination
[dequeue] =>
[value] => xxxxxxxxxxx
)
)
)
stdClass Object
(
[id] => 60dc4646-06ce-44d0-abe9-ee371847f4df
[percentage] => 68
[destinations] => Array
(
[0] => stdClass Object
(
[id] => 60046
[destination_id] => 60046
[type] => Destination
[dequeue] =>
[value] => xxxxxxxxxxxx
)
)
)
I need it to end up looking like this.
[376339] => Array
(
[0] => Array
(
[id] => 1f422730-f54b-4e4d-9289-10258ce74446
[percentage] => 32
[destinations] => Array
(
[0] => stdClass Object
(
[id] => 59826
[destination_id] => 59826
[type] => Destination
[dequeue] =>
[value] => xxxxxxxxxxx
)
)
)
[1] => Array
(
[id] => 60dc4646-06ce-44d0-abe9-ee371847f4df
[percentage] => 68
[destinations] => Array
(
[0] => stdClass Object
(
[id] => 60046
[destination_id] => 60046
[type] => Destination
[dequeue] =>
[value] => xxxxxxxxxxxx
)
)
)
)
I'm not sure if this makes any sense, that's why I had my two inital outputs I need to have merged into one somehow. This is all coming from one huge json object and I'm just using json_decode($jsonStuff) to decode it.
Would this be easier if I added true in the decode function? If I could just search for it like I could in python, that would be neat. But as it is, I'm at a loss as to how to get the output I need.
Note: Input json CANNOT be changed, I have no affiliation with the people that created it.
First loop over your input array and create an array with the key as the id
$input = json_decode($json_input);
$output = array();
foreach($input as $obj){
$output[$obj->id] = $obj;
}
then you can build your other array by searching the id on the array key
$massive_search_array = array(376339 => array
(
0 => 1f422730-f54b-4e4d-9289-10258ce74446,
1 => 60dc4646-06ce-44d0-abe9-ee371847f4df
)
);
$final_output = array();
foreach($massive_search_array as $index => $searches){
foreach($searches as $search){
if(isset($output[$search])){
$final_output[$index][] = $output[$search];
}
}
}

PHP Printing JSON value

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

Getting array key(protected)-value from an array inside object (response from RTM-php)?

When using php library for RTM (https://github.com/bartosz-maciaszek/php-rtm), I am getting a response for a particular tasks-list like this:
Rtm\DataContainer Object
(
[attributes:Rtm\DataContainer:private] => Array
(
[0] => Rtm\DataContainer Object
(
[attributes:Rtm\DataContainer:private] => Array
(
[id] => 19594773
[taskseries] => Rtm\DataContainer Object
(
[attributes:Rtm\DataContainer:private] => Array
(
[id] => 310899576
[created] => 2013-10-03T05:35:52Z
[modified] => 2013-11-06T17:24:36Z
[name] => A new task
[source] => js
[url] =>
[location_id] =>
)
)
)
)
)
)
I want to get the value of [name]. How can I do it?
As far as I could understand from the docs, you have to call something like
$receivedObject->getTaskSeries()->getName()
Or there is a suggestion to apply toArray or toJson to returned object - that should work.

Categories