I have a problem with the json functionality in zend and js.
I try to encode a single array containing some number of models like this:
echo json_encode(Application_Model_Marker::getMarkers());
var mark = JSON.parse(jsonVal); //in js
where getMarkers is a static method that returns an array of marker models.
This works fine and when I parse it in the js script and try accessing the values of the json object it works fine.
If however I try to create and send an array of array like this:
$allData = array();
$allData['info'] = Application_Model_Marker::getMarkers();
$allData['openingHours'] = Application_Model_Openinghours::getOpeningHours();
$allData['happyHours'] = Application_Model_Happyhour::getHappyHours();
echo json_encode($allData);
It still sends all the correct information when I try to alert(jsonVal.responseText); in js.
It has three arrays each containg some arrays of objects.
However when I try to initialize a variable to the parsed json object like in the first example, I can't access the values and it seems some kind of error occurs as the program stops when I try it.
I don't quite get it as it has all the correct info when i just try to print the response text from the encoded json object.
Any ideas how to do this multidimensional json encoding?
Try this, Hopefully it'll work :
<sctript>
var mark;
eval("mark = "+jsonVal+";");
</sctrip>
Related
I am getting this type of object from PHP. {"3":4} Starting array is another value which is encoded into json and echoed. And the rest is another array which is encoded into json and echoed.
How to process this in react native.
{"3":4}{"1":{"section":"8CSB","lecture_id":"24","subject_name":"MySQL","subject_code":"8CS2","date":"2020-04-29","lecture_no":"1"},"2":{"section":"8CSB","lecture_id":"25","subject_name":"Android","subject_code":"8CS3","date":"2020-04-29","lecture_no":"2"},"3":{"section":"8CSB","lecture_id":"26","subject_name":"Mobile Computing","subject_code":"8CS1","date":"2020-04-29","lecture_no":"3"},"4":{"section":"8CSB","lecture_id":"43","subject_name":"UNPS Lab","subject_code":"8CS5","date":"2020-04-29","lecture_no":"5"},"5":{"section":"8CSB","lecture_id":"45","subject_name":"UNPS Lab","subject_code":"8CS5","date":"2020-04-29","lecture_no":"6"}}
It's JavaScript at the end of the day and that's JSON - so you just parse it in JS.
var Object = JSON.parse(jsonstring);
For a more detailed explanation of what to do, try this which will go into the picker.
As for the first part, it's malformed, by the looks of things, so you cannot parse it.
To remove the first part, if it is consistently this, you can do it like so:
var Inputobject = <your input>;
var jsonstring = Inputobject.slice(7);
var Object = JSON.parse(jsonstring);
I have an array stated in a codeigniter class in libraries like
$this->myArray =("keyname1"=>"fashion bags,accessories," ,"keyname2"=>"aplaku");
It works fine for what I want to do, but the array is going to get longer as my web expands and its a pain to manage like this.
So i put the array data into into sometext.txt , in the form as "keyname1"=>"stuff","keyname2"=>"stuff"
then I put it in the extra folder of codeigniter and load it using $this->load->helper('file'); and then use
$someString = read_file('extra/data.txt'); if I echo $someString I get:
"keyname1"=>"stuff","keyname2"=>"stuff"
the next thing I want to do is $this->myArray = array($someString);
It doesn't work and the issue seems to be string to array conversion
I did once use $this->anArray = func_get_args($data); but this is only for
arrays with index [0] [1]... etc
so if
$string = "keyname1","stuff","keyname2", "stuff"; //how do I do the next line
$someArray =array( $string);
Before save you can encode the array to a json and when you retreve back you can decode json.
usejson_encode($arr) for encode to json and use json_decode ($json) when retreving.
I have an array, myData=[[1,2,3],[4,5,6],....,[..,..,..]]
I want to post this to the server.
Usually, for a JS variable, I just put the variable into a textbox and then submit the form using JS. However, when I put the 2D array into a textbox, JS converts it to a string, such that it becomes a 1D array, which looks like:
[1,2,3,4,5,6....]
I want to be able to post the entire 2D array to the server and retrieve it on the next page using PHP. How do I do that?
Thanks!
You should use JSON to turn the array into a string with javascript and then parse the string back into an array in php.
for example:
var myData = var data = [['hooray',1],['test','meow'],[0,3,2]];
var myData_string = JSON.stringify(myData);
This will turn your object/array into a string which you can then POST and parse with php like so:
$myData = json_decode($input);
References:
JSON (javascript)
json_decode() (php)
I'm fairly new to JSON, and I'm trying to get the user data from google +
JSOn Code is
https://www.googleapis.com/plus/v1/people?query=saurabh+sharma&key=AIzaSyADJjj8IeKuGb-woleHKTVouSlvAJUpTrs
Please help me to retrive the user profile.. in php
var_dump(json_decode(file_get_contents('https://www.googleapis.com/plus/v1/people?query=saurabh+sharma&key=AIzaSyADJjj8IeKuGb-woleHKTVouSlvAJUpTrs')));
It's indeed a bit of a mixed potato-puree (=confusion) between arrays and objects. That JSON is entirely object, except for the array 'items', which is an array of... objects.
Try this:
<?php
$strUrl = "https://www.googleapis.com/plus/v1/people?query=saurabh+sharma&key=AIzaSyADJjj8IeKuGb-woleHKTVouSlvAJUpTrs";
$strContents = file_get_contents($strUrl);
$objPeopleFeed = json_decode($strContents);
//It's an array of objects, so:
echo "<h1>{$objPeopleFeed->title}</h1>";
foreach($objPeopleFeed->items as $objUser)
{
echo "
<p>
<img src='{$objUser->image->url}' />
<a href='{$objUser->url}'>{$objUser->displayName}</a>
<i>{$objUser->objectType}</i>
</p>";
}
?>
What it does: It gets the contents from the web (which is JSON), interpretes it as JSON into valid PHP structures. From the structure, we print the title as a H1 header. From the items, which is an array, we loop through each one, print the image src from $objUser->image->url, print the user link $objUser->url with its name $objUser->displayName, and optionally its type of object $objUser->objectType that is registered on Google.
Because everything is kind of objects, you use the object to variable syntax '->xyz' instead of array indexes '["xyz"]', and I guess you got stuck there. The $objPeopleFeed->items is a non-associative array, so you use numbers to loop through the items ($objPeopleFeed->items[0] for the first item, 1 for the second, etc...). As a final cookie to you, you can use count($objPeopleFeed->items) as a results count.
I am sending a CSV list to the server within the url. It is a list of songid's:
var $array = [];
oTable.find('td').each(function(){
$songdata = $(this).data('data');
$songid = $songdata.songid;
//The next line is the variable I need to also send along with each songid
$duration = $songdata.duration;
$array.push($songid)
});
This results in '24,25,32,48,87' being sent to the server.
I then use the following in my php script to convert it into an array:
$songs = $_GET['songid'];
$songarray = explode(",", $songs);
This works great, I can access the data properly and all is well.
However I now have the need to add another property to each of the songid's. As seen above, I need to add the $duration variable to the data sent and be able to convert it into an array server side. (or alternatively construct the actual array client side would be fine also)
How would I go about this?
You could use this to encode your array into JSON:
http://www.openjs.com/scripts/data/json_encode.php
And then send that String to your backend where you can unpack it with this:
$yourarray = json_decode($string)
I would create an object of the data in your jscript and send it over to the PHP and then use json_decode then you have an associative array of your data within the PHP
You always can use JSON for data serialization/deserealization as of Serializing to JSON in jQuery
try sending arrays to PHP in JSON format and then do json_decode($var) in your PHP script.
make an associative array server side
$array = new Array();
//add songid as key and duration as value
$array = array_push_assoc($array, $songid, $songduration);
echo json_encode($array);
on the client side after parsing the json
$.each(json,function(key,value){
//key will be the songid
//value will be the duration
});