i am working in extjs+yii. my server side design is in yii framework and client side design is in extjs. Now from extjs i am getting output data in json format as=
{"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 frameowk how to access this json fields? i want to access questionId and optionId fields of this json. So can someone please help me
You have to decode the json using CJSON::decode, and then access the fields:
$var='{"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}]}';
$decoded=CJSON::decode($var);
// now your json is stored in $decoded as an array, so you can access as follows:
echo $decoded["data"][0]["questionId"];
echo $decoded["data"][0]["optionId"];
// to loop over the elements use foreach
foreach ($decoded["data"] as $value){
echo $value["questionId"];
echo $value["optionId"];
}
Using CJSON::decode is better than relying on native json_decode, as when the native is not available CJSON::decode can still decode a json string. Also in some cases json_decode can return null for correct json strings, read here for a comparison of php json libraries.
Related
I am sending the below POST data to an XQuery module.
When I try to read it, I have tried everything, fn:doc, from-json, etc...., I cannot find a solution. Depending of what I try to do, I either get coercion error or data is not a string.
Can anyone help me put the Artist value to an $artist variable, etc...?
Here is the post request:
curl --anyauth --user admin:admin -H "Content-Type: application/json; charset=UTF-8" -H "Accept: application/json" -X POST -g "http://localhost:8030/LATEST/resources/jsontest/" -d "{"Artist" :"bowie","Weeks" : {"gte" :"1960-01-01","lte" :"2020-12-31"},"Genres":"","Released":"" }"*
and here is my code reading the POST request:
declare function test:post(
$context as map:map,
$params as map:map,
$input as document-node()*
) as document-node()*
{
xdmp:log($input)
};
and the result is this:
{"Artist":"bowie", "Weeks":{"gte":"1960-01-01", "lte":"2020-12-31"}, "Genres":"", "Released":""}
which looks like a json document (to my untrained eye), but i can't figure out how to "read/assign" the value for Artist (in this case "bowie") and put it in a variable.
I tried fn:doc, because I thought it should be a document-node
I tried let $artist := $input/Artist/data(), but get a coercion error
I tried many other option (from-json, etc..)
but I always wind up with some sort of error
how can I get access to the values in the JSON? I've been running around in circles through the MarkLogic documentation and different searches and nothing seems to work.
The subtle differences between a document-node(), a JSON object, a JSON document stored in the database as an object-node(), and a string of JSON can be confusing. Especially when if you log it and see the stringified representation, you just see the JSON and it is difficult to tell what it really was.
When in doubt, xdmp:describe() and xdmp:type() can be helpful for that. They can help tell you what sort of "thing" you really have.
When the method interface says you will get a document-node(), that is a generic container for stuff. It will have a child node(), but that node could be lots of things. It could be an XML document and the children could be comments, processing instructions, element, or it could be a binary(), text(), or a json:object. In this case, you know that you are going to have a JSON object.
You can use XPath to select that child node:
$json := $input/node()
With a JSON Object, you can access the Artist property by using map:get()
let $artist := map:get($json, "Artist")
The difference between a JSON object and an object-node() (JSON document stored in the database within MarkLogic) is that a regular JSON Object is just a data structure, but the object-node() lives in the database and has been parsed and saved as database nodes tied to indexes, so there are extra features and functionality. After inserting a JSON object into the database, if you later retrieve it with fn:doc(), then you it will be a JSON object-node() in the database, and you can do some neat things such as use XPath against it.
xdmp:from-json-string() can be used to parse a JSON string. It will construct a JSON object (which is basically a specialized map).
You can convert that object-node() into a JSON object using xdmp:from-json().
It is also possible to serialize the json:object map as an XML structure using json:transform-from-json() and then transform that XML structure back into a json:object using json:transform-to-json().
I have a simple JSON file in AWS that has a very simple array in the format below
{
"student_ids": [5466,232,32145]
}
I am trying to reading the id's and place them in an array in ruby. I have access to the file and can access the data. I just can't figure out how to parse it into my ruby array
you can use the following
require 'json'
hash = JSON.parse('{"student_ids": [1,2,3,3] }')
p hash["student_ids"]
I am trying to access the steamid data in a json response returned by an API, specifically the Steam API.
The responses look like this:
I've made it return json but why do I see array all over the place?
How would I access the steamid data? I'm getting a bit confused as I thought this would be json.
I'm using guzzle to get the data and converting it to json using the guzzle json() method:
Any help would be appreciated.
Thanks!
The API is indeed using JSON to send/receive , however JSON is just a string, so in order to use that data PHP must parse it, which is automatically handled by guzzle, so as soon as you get the data back it has automatically decoded the data into a usable format for yourself.
It does this using the json_encode() and json_decode() functions.
You'd be able to access the steamid with the following.
// Assuming $data is your response from the API.
$players = array_get($data, 'response.players', []);
foreach($players as $player)
{
$steamId = array_get($player, 'steamid', null);
}
Using the laravel helper array_get() function is a great way of ensuring you return a sane default if the data doesn't exist as well as eliminating the need to keep doing things like isset() to avoid errors about undefined indexes, etc. http://laravel.com/docs/5.1/helpers
Alternativly not using the laravel helpers you could use something similar to below, although I'd advise you add checks to avoid the aforementioned problems.
foreach($data['response']['players'] as $player)
{
$steamId = $player['steamid'];
}
If you didn't want guzzle to automatically decode the API's JSON I believe you should just be able to call the getBody() method to return the JSON string.
$json = $response->getBody();
I am creating project in Yii framework. In controller i have function as-
public function actiongetCuriosityQuestionAnswer()
{
$model=new Curiosityquestion;
$json='{"questionId":1}';
$obj=json_decode($json);
$model->questionId=$obj->questionId;
$record=Curiosityquestion::model()->findByPk($model->questionId);
echo "The Question=".$record->question."</br>"."Its answer is-".$record->answer;
echo CJSON::encode($record->answer,$record->question);
}
I want to send question and answer in json format. But "CJSON::encode($record->answer,$record->question);" is sending only answer in json format. i.e. CJSON::_encode is accepting only one parameter. So how to send both question and option in json format together?
Pass your whole object to CJSON::encode, e.g.:
echo CJSON::encode($record);
If you want specific elements, pass as an an array, e.g.:
echo CJSON::encode(array('answer'=>$record->answer, 'question'=>$record->question));
In your javascript, you can access the elements as javascript objects, e.g. data['answer'] or something similar
As you all know there is an Export "Button" when you Click(in Drupal) on your View(e.g. Customer Data etc.). So I'd like to visualize somehow the data I geht here(which is unfortunately JSON Objects in PHP Style). Most Viewers & Readers (e.g. this here http://jsonviewer.stack.hu/ or some other SW solutions) can't read this Type of JSON!
I'm aware of the possibility of parsing this Exported JSON Data in php but I'm lookin for a more friendly solution.
Thanxx out there!
George
It's not 'PHP style JSON' (I'm quite sure such a thing doesn't exist) it's a PHP array!
Either grab the views datasource module which can export views as JSON/XML natively, or simply run the PHP output through json_encode().
If the output looks like this:
$view = array(
'something' => 'something',
...
);
Then you need to run $string = json_encode($view); echo $string;.
That will output the View as a JSON object/array :-)