Codepolice

Random posts from Ola in English. Mainly about programming and the web.

Great tools to test JSON and (totally unrelated) Pingbacks

I’ve done lot’s of “web 2.0″ stuff lately and i just want to write down to really nice tools i found.

Test and Validate JSON

I have just built an API for AlternativeTo. It use JSON and this tool was really a life saver when it came to debug and test everything.

http://jsonformatter.curiousconcept.com/

Test if you website handles Pingbacks correctly

Right now i am implementing a pingback functionality on AlternativeTo and i found this little website that lets you sent pingbacks without having to create a new WordPress post each time you want to test if everything works. Great stuff.

http://www.solitude.dk/ping/client.php

Return random rows from a MS SQL database

I always forget this command:

SELECT * FROM YourTable TABLESAMPLE SYSTEM (10 PERCENT)

This gives you some sample data from your database. In this case 10 percent of the content.  You could also use TABLESAMPLE SYSTEM (1000 ROWS) to get 1000 sample rows. Great to use if you have a big database and do not want the same data again and again when you debug or something.

Convert String Array to Int Array and vice versa in C#

Sometimes you want to convert arrays back and forth. This is to nice extension methods to convert a int array into a string array and vice versa.

public static class ExtensionMethods {
 
  public static string[] ToStringArray(this int[] intArray) {
   return Array.ConvertAll<int, string>(intArray, delegate(int intParameter) { return intParameter.ToString(); });
  }
 
  public static int[] ToIntArray(this string[] strArray) {
   return Array.ConvertAll<string, int>(strArray, delegate(string intParameter) { return int.Parse(intParameter.ToString()); });
  }
}

Convert all checked checkboxes into an comma seperated string with JQuery

Sometimes i just love jQuery. Well most of the time actually. I did some work for a client a couple of week ago and i needed get all checkoboxes that was checked as a comma seperated list. I started with this code. I found out about the “map” method of jQuery wich has the following description.

Translate all items in an array to another array of items.

The translation function that is provided to this method is called for each item in the array and is passed two arguments: The the item to be translated, and index within the array.The function can then return the translated value, ‘null’ (to remove the item), or an array of values – which will be flattened into the full array.

The code i ended up with looked like this. Really really neat.

 
        $(document).ready(function () {
            CheckSelected();
        });
        $('.chk').click(function () {
            CheckSelected();
        });
        function CheckSelected() {
            var idArray = $('.chk:checked').map(function () {
                return $(this).val();
            });
            $("#show").text($.makeArray(idArray).join(','));
        }