I am using jQuery widgets in a Joomla application. The data for the widget must an object and in json format. I am able to achieve that when I retrieve the data direct from the database without the database functionality of the Joomla framework.
Of course, I do want to do it within the Joomla MVC structure and I want to utilise the Joomla database functions. When doing it there is no output in the widget.
I had this before that when passed as an array, it does not display the data, when passed as an object, it does.
My problem is this: Generating the data directly from the database and using the Joomla database structure produces exactly the same result when echoed to the screen but one is displayed in the grid and the other isn't. That was also the case with the previous problem which I had but in that case I called json_encode twice on the same data but there was absolutely no difference in the displayed data, but one works and the other doesn't.
My question: I am not that familiar with JSON data yet but is there a way to check whether the data is represented as an object or an array? This maybe a very dumb question but it seems to me that there must be a difference in the data and by simply looking at it, you are not able to see it, or am I missing something?
UPDATE: Thanks for the responses. I have sat on this for days!! It is working now with the Joomla database functions, it seems there was a minor issue with my code apart from the json encoding but they looked exactly like this when I used the json_encode both in the model and the controller before, therefore double json encoding it, which did not work but when generated outside of Joomla, it worked (single use of json_encoding). Looking at them generated outside of Joomla and after a double json encoding, both of the looked the same, therefore I could not found the problem looking at the output, the same as now.
Thanks for the info regarding the [] and {}, that is insightful but here is an example of my code:
[{"TotalRows":2,"Rows":[{"login_id":"122","cust_id":"0","shop_id":"0","nickname":null,"shopicon":null,"website":null,"shopname":null, "username":"","password":"","dob":"0000-00-00","comments":null},
{"login_id":"25","cust_id":"57","shop_id":"42","nickname":"qwerty","shopicon":"shop.ico","website":"http:\/\/www.shop.co.uk","shopname":"Shop","username":"eqweq","password":"wqewqeq","dob":"1981-12-14","comments":"qwqeqeqw"}]}]
How can I tell from this whether it is an array or an object?
$db->setQuery($query);along with$result = $db->loadObject();, now you've got a php object. simply using echojson_encode($result);will output a json object to the client, which can then be parsed using$.parseJSON, or if you're using ajax,set the dataType = "json"and it will automatically parse it. then you can access object properties byobj.prop' '. This is a JSON string and NOT an object.{or[. If you already parsed the JSON into native JavaScript data types, then you have to perform a different test, which most likely has already been asked before. So, what is it? JavaScript or JSON?