1

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?

6
  • 2
    {} = object, [] = array. A sample would be helpful. Also, you can check the syntax here: jsonlint.com Commented Jun 22, 2012 at 14:09
  • Seems decently straight forward...Joomla database structure isn't that difficult. You need to use $db->setQuery($query); along with $result = $db->loadObject();, now you've got a php object. simply using echo json_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 by obj.prop Commented Jun 22, 2012 at 14:10
  • Elaborating more on what @Diodeus said...{} is an object, however, it can be a string as well, if it is encased in ' '. This is a JSON string and NOT an object. Commented Jun 22, 2012 at 14:16
  • 1
    The problem is that people are always confusing JavaScript array/object literals with JSON. Make sure you understand the difference. That said, if you have JSON, you can easily test whether the toplevel structure represents an object or array by testing whether the first character is a { 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? Commented Jun 22, 2012 at 14:37
  • It is JSON and looking at my code and the comments, I believe I have an array of objects. Commented Jun 22, 2012 at 14:50

4 Answers 4

3

The following code use to get all keys and checks where associated values are in form of array or json object or string value.

Iterator<?> rootIter = jsonData.keys();
while (rootIter.hasNext())
{
   String name = (String) rootIter.next();
   Object obj = jsonData.get(name);
   if (obj instanceof JSONObject)
   {
      JSONObject jsonObj = (JSONObject) obj;
   }
   else if(obj instanceof JSONArray)
   {
       JSONArray jsonArray = (JSONArray) obj;
   }
   else
   {
      String values = obj.toString();
   }           
}
Sign up to request clarification or add additional context in comments.

Comments

1

If you have JSON string, you can simply check if first non-whitespace symbol is { or [. { would mean that top level is regular object (remember that arrays are objects too!), [ - array. You can also parse it to object and check if instanceof Array is true for the result.

3 Comments

Therefore [{ means an array of objects?
Array with object as it's first element. It could easily be followed with next element as array, or simple number or string.
OK! So [{},{},{},{},{}] is an object of arrays? and [{[{}, {}, {}, {}, {}]}] is an array that contains one object and this object contains array of objects?
0

They should be very different:

var arr = [1, 2, 3, 4];
console.log(JSON.stringify(arr)); // prints "[1,2,3,4]"
var obj = { a: 1, b: 2, c: 3, d: 4 };
console.log(JSON.stringify(obj)); // prints "{"a":1,"b":2,"c":3,"d":4}".

Can you please show some inputs and outputs (and some code) so we can see what is happening?

Comments

0

Sorry user1154041, this is not exactly an answer, but it might help you see the wood for the trees. When developers get a piece of JavaScript (or the subset JSON) ready for deployment they "minify" it, which means to remove all whitespace. It makes no difference to the JavaScript engine, but it's hard for us humans to read. Re-formatting a minified piece of code is sometimes called 'prettify' and the process used to need special tools. These days all the editors I use have a 'format' function which takes a string of JavaScript or JSON like your example and formats it in a much more readable form like the example below (I had to add the var statement to make it valid JavaScript so that my editor understood what to do):

var test = [{
    "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"
    }]
}]

I know you still have to get used to the fact that [] indicates an array and {} an object, but don't you agree it's much easier to see? So, when you're puzzled by a string like that, open up an IDE like Aptana http://www.aptana.com, designed for editing JavaScript. Create a JavaScript file, assign the string to a var and hit the format button. That way you have half a chance of figuring out the structure of the code.

2 Comments

OK! So [{},{},{},{},{}] is an object of arrays?
@user1154041 Exactly! It's much easier to see now, isn't? But, as Oleg pointed out, an object could easily be followed by a string or an array. e.g. [{}, {}, "hello", ["a","b"]}. Each element of a JavaScript array can be any type you like. JSON is a bit stricter but basically the same idea. Whatever it is, prettifying or formatting makes it easier to figure out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.