Saturday, March 2, 2013

Working with Arrays

So this is an example of the returned json for 'get_info' request
{"success":"1","info":{"created":"2012-8-30 16:47:59","country":"Ghana","user_id":"2","onlinestatus":"1","last_login_datetime":"2013-3-2 22:13:10","nickname":"badu","discover":"0"},"interests":[" cook","eat"]}
Interests is an array of values. The doc specifies updating interests at such
interests - comma separated list of interests 
Obviously, a suitable method for converting between arrays and a comma separated string value(s) is required. Unfortunately for Java and by extension J2ME, they never bothered to think that people might implementations similar to split and join in Python. So here goes..

1. Split first. Very simple method for splitting a string into an array along defined separators

public String[] split(String original, String separator)
                {
                    Vector nodes=new Vector();
                    //Parse nodes into Vector
                    int index=original.indexOf(separator);
                    while(index>=0){
                        nodes.addElement(original.substring(0, index));
                        original=original.substring(index+separator.length());
                        index=original.indexOf(separator);
                    }
                    //Get the last node
                    nodes.addElement(original);
                    //Create split array
                    String[] result=new String[nodes.size()];
                    if(nodes.size()>0){
                        for(int loop=0; loop<nodes.size(); loop++)
                        {
                            result[loop]=(String)nodes.elementAt(loop);
                        }
                    }
                    return result;
             }//--End of split()
     
2. Now the slightly harder part. Convert the returned array in the json response to a string. First problem here is that getting the array involves returning it as a JSONArray which apparently is not castable to a normal String Array. So this is how you do it...
            //Convert JSONArray to String Array
            JSONArray key_array = json.getJSONArray("interests");
            String[] key_attributes = new String[key_array.length()];
            for(int i=0; i<key_array.length(); i++){
                key_attributes[i] = key_array.getString(i);
            }
 This is followed by the join method which I implemented as such:

public String join(String[] array, char separator){
        //Catch empty array
        if (array == null) return null;
        if(array.length == 0) return null;
       
        String joined_string = "";
        for(int i=0; i<array.length;){
            joined_string += array[i] + separator;
            i++;    //Don't forget to increment
        }
        //Truncate trailing seprator
        return joined_string.substring(0, joined_string.length()-1);
    }//--End of join()
Now calling the method join, you MUST remember to provide the separator as a character. In java, it's not easy to type out a character variable in code and instead, one has to go around like this:
    String separator = ",";
            interests = join(key_attributes, separator.charAt(0));
There you go folks, all done with Java's usual unfriendly long way of getting simple things done.!

No comments:

Post a Comment