how to get an empty Json object with Zend_json? - json

From http://json.org/:
an empty Json object is:
{}
I've tried to get it with json_encode (which is officially part of PHP):
json_encode((object)(array()))
that's what I need. But somehow I have to use Zend_json to get it:
Zend_Json::encode((object)(array()))
but the result is:
{"__className": "stdClass"}
Any ideas?
My PHP version 5.1.6; ZF version 1.7.2

For me this works perfectly:
echo '<pre>'; print_r(Zend_Json::encode((object)array())); echo '</pre>'; exit;
// Output: {}
Tested with ZF-Version 1.11.3
Also possible:
Zend_Json::encode(new stdClass());

Try
Zend_Json::encode(array());

Just in case anybody is still wondering, the internal encoder of the ZF adds the __className property to each object.
The internal encoder is used if the PECL extension json is not installed and thus the function json_encode not available (see http://php.net/manual/en/function.json-encode.php ).
Just use
preg_replace('/"__className":"[^"]+",/', '', $jsonString);
to get rid of all className elements

I find the solution as below:
$m = Zend_Json::encode($obj);
$res = str_replace('"__className":"stdClass"', '', $m);
$res = str_replace("'__className':'stdClass'", '', $res);
$res = str_replace("'__className': 'stdClass'", '', $res);
$res = str_replace('"__className": "stdClass"', '', $res);
return $res;

To get around this in Zf2 I have added a disableClassNameDecoding option to Zend\Json\Encoder.
If you want to disable the __className output, you can use it like this:
return Zend\Json\Json::encode($object, false, array('disableClassNameDecoding' => true));
The patched file can be found on github. At some point I will add unit tests and create a pull request.

Related

How to retrive the parameters in URL + laravel

for web services , how to retrieve the data json format using Laravel, can anybody help
https://test.com/admin/users/apiLogin?Login={"email":test#test.com,"pwd":testing}
in Controller
public function apiLogin(Request $request)
{
//$data = $request->json()->all();
$data=json_decode($request['Login']);
echo $data['email'];
echo "<pre>";
print_r($data);
echo "</pre>";
echo "testsetet";
exit;
}
To get the URL parameter use this:
$login = $request->input('login'); // or $request['Login']
The goal is to turn login (which is a string) into appropriate JSON format so that you can use json_decode.
I'll break it down in steps:
Remove all \n characters:
$login = str_replace('\n', '', $login);
Remove last comma
$login = rtrim($login, ',');
Add brackets
$login = "[" . trim($login) . "]";
Turn it into a PHP array:
$json = json_decode($login, true);
The issue here is that your url parameter 'Login' isn't properly formatted json. I would revisit your front-end code persisting this data and ensure it is formatting properly. True json should look like:
{"email":"test#test.com","pwd":"testing"}
As a BIG aside, never persist passwords in the URL. It is dangerously foolish. Use a POST request to do this. On the plus side, the back-end code will be virtually the same with your json_decode() logic.

Get variable GET in Yii2 not working

Basiclly I have an url that have a parameter.
So, I need to access those parameter using GET. This is the GET that coming:
Array
(
[r] => hanwa/incoming-pipe/assign-incoming
[1] => Array
(
[min_urut] => 1
[max_urut] => 44
)
[_] => 1496645704980
)
In docs, docs,
We can use like this :
echo $request->get('min_urut');
But, I got nothing. Please Advise.
Use Concept of ArrayHelper class :-> http://www.yiiframework.com/doc-2.0/guide-helper-array.html#getting-values
ex:
$data = ArrayHelper::getValue($request->get(), 'Temp.yourvalue.yourindex');
Main use of $request->get() method mainly returns to you a value from $_GET,
so example is
$temp = $request->get('Temp'); // Here $temp variable contains $_GET['Temp']
$data = $temp['yourvaluename'][0];
Judging by the fact that you have an array with the key [r] you have an associative array, method get() you will not get it, you need to do it just like this ..
echo $request->get()[1]['min_urut'];

CakePHP jSon format change

I'm finding list of Areas for my CakePHP 2.x website and it supports, JSON output as below with find all method:
$this->Area->find('all', array('fields' => array('id', 'name', 'order'), 'conditions' => array('Area.status' => 1)));
Below is my JSON output:
[{"Area":{"id":"2","name":"Area 1","order":"1"}},{"Area":{"id":"3","name":"Area 2","order":"1"}}]
Now Is that possible for me to remove Area tag which is repeating everytime?
Any patch for same? Do let me know if any suggestions / ideas.
on your view for output json write this:
echo json_encode(Set::extract('/Area/.', $area));
For me works fine.
CakePHP Provides some in-built library functions for data extraction from result set and output same as JSON format.
// initialize your function to render false and response type json
$this->autoRender = false; // no view to render
$this->response->type('json');
// Remove Area from array and encode
$area = Set::extract('/Area/.', $area);
$json = json_encode( $area);
$this->response->body($json);
Hope it helps !

Soundcloud API doesn't explicitly support pagination with json

Specific example I was working with:
http://api.soundcloud.com/users/dubstep/tracks.json?client_id=YOUR_CLIENT_ID
You'll get their first 50 tracks, but there is not next-href object like what you see in the xml version.
However, you can use offset and limit and it works as expected- but then I would need to "blindly" crawl through tracks until there are no more tracks, unlike with the XML version which gives you the "next page" of results. I wouldn't have even noticed it was paginated except by chance when I was searching the json object and noticed there was exactly 50 tracks (which is suspiciously even).
Is there a plan to support the next-href tag in json? Am I missing something? is it a bug that it's missing?
There is an undocumented parameter you can use linked_partitioning=1, that will add next_href to the response.
http://api.soundcloud.com/users/dubstep/tracks.json?client_id=YOUR_CLIENT_ID&linked_partitioning=1
for ex :
// build our API URL
$clientid = "Your API Client ID"; // Your API Client ID
$userid = "/ IDuser"; // ID of the user you are fetching the information for
// Grab the contents of the URL
//more php get
$number="1483";
$offset=1300;
$limit=200;
$soundcloud_url = "http://api.soundcloud.com/users/{$userid}/tracks.json?client_id={$clientid}&offset={$offset}&limit={$limit}";
$tracks_json = file_get_contents($soundcloud_url);
$tracks = json_decode($tracks_json);
foreach ($tracks as $track) {
echo "<pre>";
echo $track->title . ":";
echo $track->permalink_url . "";
echo "</pre>";
}
sI've seen this code is supposed to help (this is in Ruby):
# start paging through results, 100 at a time
tracks = client.get('/tracks', :order => 'created_at', :limit => page_size,
:linked_partitioning => 1)
tracks.each { |t| puts t.title }
However, the first set of results will show and i'll even see the "next_href" at the end of the response, but what are you supposed to do, to make the next set of results show?

How to use node_load()?

Hi m a bit confused that how to retrieve node title by using this code
node_load($nid);
$title=$nid->title;
i have done this coding in block and i wants to retrieve from node id for displaying image.that images are normally uploaded at the site by using filezilla and it has same name as the node title.i have tried many forms of node_load(),but i m failure.so please tell me right option for this.
Thanks all.-Pranoti
Here is the reference for node_load
http://api.drupal.org/api/function/node_load
It returns an object which is the node.
$node = node_load($nid); // $nid contains the node id
$title = $node->title;
Please get a good book on Drupal Module development to learn the fundamentals.
Your question is a little confusing. Could you clean it up and explain better what you are trying to accomplish? In all events:
Node load takes either an numeric argument or an array of parameters to query, and returns a single node object. (As already mentioned, here's the API documentation: http://api.drupal.org/api/function/node_load).
Load with a numeric node id:
$nid = 55;
$node = node_load($nid);
$title = $node->title;
Load by querying on title:
$title = 'How to serve man';
$node = node_load(array('title' => $title));
$body = $node->body;
you can also load multiple node load efficiently by using the following code
<?php
$type = "product_type";
$nodes = node_load_multiple(array(), array('type' => $type));
foreach($nodes as $products):
?>
<?php print $products->nid; ?>
<?php print $products->title; ?>
<?php endforeach; ?>
also you can query any thing in the node load for example we have used type in query but we can also use title as mentioned in the above post by
"David Eads"
NODE LOAD BEST PRACTICES
If you are loading a lot of nodes with node_load(), make sure to use the $reset parameter so that every node isn't kept in the function's static cache (and increasing memory usage):
$nid = 55;
$node = node_load($nid, NULL, TRUE);