Var dumping a JSON object with laravel with JSON Formatter - json

I'm trying to figure why I whenI browse to a page that has JSON data it won't display it in the JSON formatter with the jSON Formatter plugin installed on my local computer. Anybody know why this could be the issue? What are some possibilities for me to look into?
/**
* Display a listing of users
*
* #return Response
*/
public function index()
{
// Retrieve all users from database with roles and statuses
//$users = User::with('role')->with('status')->get();
$users = User::all();
var_dump($users);
// Return a view to display all users by passing users variable to view.
//return View::make('users.index', compact('users'));
}

I'm not familiar with the tool your post mentions, but when you say
var_dump($users);
you're not returning JSON. You're returning an HTML page that contains text contents that looks like dumped JSON.
I'm going to guess that the "jSON Formatter" plugin you mentioned looks for property JSON response headers to decide if it should handle a request or not. Try the following instead.
public function index()
{
// Retrieve all users from database with roles and statuses
//$users = User::with('role')->with('status')->get();
$users = User::all();
return Response::json($users);
}

Related

How to avoid html entities like – in JSON data in WP REST API?

In wordpress site while fetching JSON data using WP REST API how to decode &#8211, &#8220, etc.
Some latest android webview fails to decode it.
Add this code in your theme functions.php , it acts as a middleware to resolve entities before serving it to the API.
function fix_decode_rest_api($response, $post, $request) {
if (isset($post)) {
$decodedTitle = html_entity_decode($post->post_title);
$response->data['title']['rendered'] = $decodedTitle;
$decodedPostTitle = html_entity_decode($response->data['title']['rendered']);
$response->data['title']['rendered'] = $decodedPostTitle;
}
return $response;
}
add_filter('rest_prepare_post', 'fix_decode_rest_api', 10, 3);
Just decode the string in the request instead of calling the database all over again. You already have the content to decode in $response
$response->data['title']['rendered'] = html_entity_decode($response->data['title']['rendered']);

How do I display json get result using Wix Code?

I'm working with a non-profit cat shelter trying to update their website. They want to have a page that connects to their shelter manager software to display the available cats for adoption. Luckily, their shelter manager offers API calls to get the information I need from it.
They use Wix as their platform and are pretty set on keeping it as most of their volunteers know how to make easy adjustments to other pages. I researched and found Wix offers the ability to connect to the API using their fetch method.
Basically, I am trying to get a dynamic page to display a repeater that is populated from their json API Get method.
Currently, for the backend I have (URL to API removed for security):
import {fetch} from 'wix-fetch';
export function getdata(){
return fetch('URL to API Service', {method: 'get'})
.then( (httpResponse) => {
if (httpResponse.ok) {
return httpResponse.json();
}
} );
}
On the page, this is where I think I am getting stuck:
import {getdata} from 'backend/fetchCats';
getdata()
.then(json => {
console.log(json);
var catData = json;
// static repeater data
$w.onReady(function () {
// handle creation of new repeated items
$w("#repeater1").onItemReady( ($item, itemData, index) => {
$item("#text23").text = itemData.ANIMALNAME;
} );
// set the repeater data, triggering the creation of new items
$w("#repeater1").data = catData;
} );
});
The above is giving me the error: Wix code SDK error: Each item in the items array must have a member named _id which contains a unique value identifying the item.
I know the JSON call has an ID field in it, but I am guessing Wix is expecting an _id field.
Am I just doing this wrong? Or am I missing something simple? I've spent a couple nights searching but can't really find a full example online that uses Wix's fetch method to get data via my HTTPS Get.
Thanks for any help!
You are doing fine.
You are getting the error from the line $w("#repeater1").data = catData;
which is the line used to set the items into the repeater. A repeater expects to have a _id member for each of the items, and your data quite probably does not have such an attribute.
I assume the API you are using, when returning an array, each item has some identifying attribute? if so, you can just do a simple transform like -
let catDataWithId = catData.map(item => {
item._id = item.<whatever id attribute>;
return item;
});
$w("#repeater1").data = catData;

Having trouble retrieving JSON from res

I'm using node, express, and mongoose.
I have a function that performs a search in a database and sends the response in a JSON format with:
res.jsonp(search_result);
This displays the correctly returned result in the browser as a JSON object. My question is, how do I get that JSON object?
I've tried returning search_result but that gives me null (possibly because asynchronous). I'd also like to not edit the current function (it was written by someone else). I'm calling the function and getting a screen full of JSON, which is what res.jsonp is supposed to do.
Thanks in advance.
Just make a new function that takes this JSON as parameter and place it inside the old one. Example:
// Old function
app.get('/', function(req,res){
// recieve json
json_object = .....
// Get json to your function
processJson(json_object);
// Old function stuff
.
.
.
});
function processJson(json_object) {
// Here you'll have the existing object and can process it
json_object...
}

How to access multivalue json output from extjs in yii

i am working in extjs+yii framework. My client side is in extjs and server side design is in yii framework. So extjs's forms inputs are coming to yii framewok via json. This json is accepted in yii controller action as-
public function actionSetUserAnswer()
{
$postData = json_decode(file_get_contents("php://input"), true);
$clientData = $postData['data'];
}
Actual json data send by extjs is-
'{"data":[{"optionId":"","questionId":"1","isAnswer":"","option":"Aus","media":"","keyword":"","mediaTypeId":"","id":null},{"optionId":"","questionId":"2","isAnswer":"","option":"india","media":"","keyword":"","mediaTypeId":"","id":null},{"optionId":"","questionId":"3","isAnswer":"","option":"England","media":"","keyword":"","mediaTypeId":"","id":null},{"optionId":"","questionId":"4","isAnswer":"","option":"Srilanka","media":"","keyword":"","mediaTypeId":"","id":null}]}';
So in yii how to access these individual fields of json like option,questionId etc using $clientdata variable.
You can just use array subscripts
$option = $clientData[0]['option']
or
$questionId = $clientData[0]['questionId']
If you want to retrieve multiple values for use in findByAttributes, then you need an array of values. You first retrieve the questionIds from JSON
function get_question_id($elem) {
return $elem['questionId'];
}
$questionIds = array_map('get_question_id', $clientData);
and then use this array in findByAttributes
$record=Question::model()->findByAttributes(array("questionId"=>$questionIds));
Update incorporating your last comment:
static public function get_question_id($elem)
{
return $elem['questionId'];
}
public function actionSetUserAnswer() {
$clientData = '{"data":[{"optionId":"","questionId":"1","isAnswer":"","option":"Au‌​s","media":"","keyword":"","mediaTypeId":"","id":null},...]}';
$obj = json_decode($clientData['data']);
$questionIds = array_map('get_question_id', $obj);
var_dump($questionIds);
$records = Question::model()->findByAttributes(array("questionId"=>$questionIds));
}

Zend Jquery Autocomplete populate from Database

Hi I am trying to implement a autocomplete field using Zend Jquery. I followed a tutorial to grab the data from an array and I have extended the code to access the data from my mysql table.
IndexController.php
$this->view->autocompleteElement = new ZendX_JQuery_Form_Element_AutoComplete('ac');
$this->view->autocompleteElement->setLabel('Autocomplete');
$this->view->autocompleteElement->setJQueryParam('source', '/index/city');
This calls the cityAction()
public function cityAction()
{
$results = Application_Model_City::search($this->_getParam('term'));
$this->_helper->json(array_values($results));
}
I then call the Model City
public static function search($term)
{
$region = new Application_Model_DbTable_Regions();
$results = $region->getRegion($term);
return $results;
}
And finally the Regions db model
public function getRegion($term)
{
$select = $this->select()->from($this,'City')
->where('City LIKE ? ',$term.'%');
return $this->fetchAll($select)->toArray();
}
Now when I go the autocomplete field it shows the results but as UNDEFINED , I think its something to do the way I am send the data back to the json helper.
I used firebug and I can see the data is been pulled in the following format.
[{"City":"London"},{"City":"Londonderry"},{"City":"Longfield"},{"City":"Longhope"},{"City":"Longniddry"}]
I think this format is incorrect, please any body dealt with this before?
Cheers
J
The ZendX_JQuery_Form_Element_AutoComplete element is a proxy to the AutoComplete View Helper, which is a proxy to the jQuery UI Autocomplete widget.
If you read the overview on the jQuery UI Autocomplete page, you will note:
The local data can be a simple Array of Strings, or it contains Objects for each item in the array, with either a label or value property or both. The label property is displayed in the suggestion menu. The value will be inserted into the input element after the user selected something from the menu. If just one property is specified, it will be used for both, eg. if you provide only value-properties, the value will also be used as the label.
When a String is used, the Autocomplete plugin expects that string to point to a URL resource that will return JSON data. It can be on the same host or on a different one (must provide JSONP). The request parameter "term" gets added to that URL. The data itself can be in the same format as the local data described above.
So, the JSON you are returning to the autocomplete should be structured more like:
[{"label":"London","value":"London"},{"label":"Londonderry","value":"Londonderry"},{"label":"Longfield","value":"Longfield"}]
Good luck!
Fixed the problem afters hours of pain!
I modified the below code where it accesses the data
public function getRegion($term)
{
$select = $this->select()->from($this,'City')
->where('City LIKE ? ',$term.'%');
return $this->fetchAll($select)->toArray();
}
I added a line of SQL City AS Value
public function getRegion($term)
{
$select = $this->select()->from($this,'City AS value')
->where('City LIKE ? ',$term.'%');
return $this->fetchAll($select)->toArray();
}
This seems to have worked!
Cheers
J