To develop this extension I started from . That extension wasn't fitting my own requirements so I just developed a new and fresh one!
Requirements ¶
Was developed using Yii 1.1.4
Install ¶
Extracxt the json directory to your protected/extensions/ directory
Usage ¶
The first parameter can be a single model or an array of models. The other two parameters has to be array.
echo $json->toJSON( mixed $model, array $relations, array $attributes);
Yii::import('application.extensions.json.CArJSON');
$users = User::model()->findAll($criteria);
$json = new CArJSON();
// Define which relations do you want to load
$relations = array('posts', 'comments', 'votes');
// Define which attributes, foreach relations do you want to load
// root denote attributes for the main model
$attributes = array(
'root' => array('id', 'username', 'email', 'first_name', 'last_name'),
'posts' => array( 'id', 'author_id', 'content' ),
'comments' => array( 'id', 'author_id', 'content' ),
'votes' => null, // This will load all attributes
);
echo $json->toJSON($users, $relations, $attributes);
note
The link for the project page is wrong - http://www.yiiframework/
Thanks StErMi
Hi, StErMi I want a way to pass the data of the model from json to javascript where I have list list with the item_name from the item model. The same as your example but wants to pass the json to javascript. Thanks.
Alternative output style
In order to have an object that more closely resembles the Active Record, you can change the
$jsonDataSource = array('attributes'=>$attributes,'relations'=>$this->relations);
line in the getJSONModel to
$jsonDataSource = CMap::mergeArray($attributes, $this->relations);
try njson
http://www.yiiframework.com/extension/njson/
JQUery parse
Extension Work nicely.. the output (JSON) comes up like this :
[ {"attributes":{"id_tp":"199","id_posttag":"164","id_usertag":"29","id_group":null,"new":"1"},"relations":{"idPost":{"id_post":"164","notes":"asdsad"}}}, ]
But...I have a problem when im using JQuery to parse this JSON (output).
that output shows up like this :
is there anybody can help me??how using the jquery code to get this JSON and parse it?
this is my code that use for parse the JSON
$(document).ready(function(){ var session = window.localStorage.getItem('example'); var obj = jQuery.parseJSON(session); var idnya = obj.id_user; $.get("http://192.168.12.3/leapdeck.com/android/posting/tagpost", { idUser:idnya }, function(data,status) { var tagpost = data; var tag = jQuery.parseJSON(tagpost); alert(tag); }); });
Thanks for helping me anyway :)
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.