iPhone to Android NSMutableDictionary NSArray to HashMap ArrayList

My first post here :)

I have a summer job doing a bit of coding - I'm definitely not a pro, yet!

I'm converting this iPhone app to Android, it's nearly done. Just struggling converting this piece.

The array of 'participants' gets passed to a server via JSON. This to me, in Android, looks like it would be an array of HashMaps. Is that correct?

Here is the Objective C:

NSMutableArray *participants = [[NSMutableArray alloc] init];

NSArray *listOfParticipants = [[game getListOfGameParticipants]allValues];

for (Player *playerObj in listOfParticipants)
{
    NSMutableDictionary *playerToAdd = [[NSMutableDictionary alloc] init];
    if ([playerObj.email length] == 0) {
        [playerToAdd setValue:playerObj.phoneNumber forKey:@"Id"];
    }
    else
    {
        [playerToAdd setValue:playerObj.email forKey:@"Id"];
    }

    [playerToAdd setValue:playerObj.firstName forKey:@"FirstName"];
    [playerToAdd setValue:playerObj.lastName forKey:@"LastName"];
    [playerToAdd setValue:playerObj.phoneNumber forKey:@"PhoneNumber"];

    [participants addObject:playerToAdd];

    [playerToAdd release];
}

The getListOfGameParticipants is:

- (NSDictionary*) getListOfGameParticipants
{
return participants ;
}

and the Android code I've attempted is:

ArrayList<Map<String, String>> list = new ArrayList<Map<String, String>>();
Map<String, String> playersObject = new HashMap<String, String>();
            playersObject.put("Id", "test@test.net");
            playersObject.put("FirstName", "Test Firstname");
            playersObject.put("LastName", "Test Surname");
            playersObject.put("PhoneNumber", "Test Number");
            list.add(playersObject);
json.put("ParticipantIds", list);

Please help :)

Edit: It doesn't work. The server reports back:

The server encountered an error processing the request. The exception message is 'Expecting state 'Element'.. Encountered 'Text'  with name '', namespace ''. '. See server logs for more details.

The data type expected is:

[DataMember]
public Player[] ParticipantIds { get; set; }

Edit: Further update:

I now have my array of hashmaps parsing to a JSON string in a good format. I need to parse that JSON string to a JSONObject and put that JSONObject in a JSONArray.

The following code doesn't work:

Map<String, String> playersObject = new HashMap<String, String>();
            playersObject.put("PhoneNumber", "TestNumber");
            playersObject.put("Id", "test@test.net");
            playersObject.put("FirstName", "Test Firstname");
            playersObject.put("LastName", "Test Surname");
            ArrayList<Map<String, String>> list = new ArrayList<Map<String, String>>();
            list.add(playersObject);
            String jsonValue = org.json.simple.JSONValue.toJSONString(list);
            JSONObject hashMapObject = new JSONObject();
            hashMapObject.getString(jsonValue);
            JSONArray hashMapArray = new JSONArray(jsonValue);
            hashMapArray.put(hashMapObject);

It gives the error:

org.json.JSONException: No value for [{"LastName":"Test Surname","FirstName":"Test Firstname","Id":"test@test.net","PhoneNumber":"TestNumber"}]

Help!

Answers


The Android code does not do the same thing as the iPhone code, but as far as the data structure goes it seems correct. Just compare the JSON generated by the two methods and you'll know what's wrong.

This is the JSON code the iPhone is generating:

[
    {
        "Id" : "value",
        "FirstName" : "value",
        "LastName" : "value",
        "PhoneNumber" : "value"
    },
    {
        "Id" : "value",
        "FirstName" : "value",
        "LastName" : "value",
        "PhoneNumber" : "value"
    },
    {
        "Id" : "value",
        "FirstName" : "value",
        "LastName" : "value",
        "PhoneNumber" : "value"
    }
]

Just make sure the Android code is generating the same thing.

EDIT:

The code for manually generating JSON in Java would look something like:

String json = "[";

for (Player player : participants) {
    json += "{";
    json += "\"Id\":\"" + (player.email.length() > 0 ? player.email : player.phoneNumber) + "\",";
    json += "\"FirstName\":\"" + player.firstName + "\",";
    json += "\"LastName\":\"" + player.lastName + "\",";
    json += "\"PhoneNumber\":\"" + player.phoneNumber + "\"";
    json += "},";
}

json = json.substring(0, json.length()-1) + "]";

I don't write Java code very often, but I think the above generates the same JSON as the Objective-C code you posted. It's not indented, but I don't think the server will care.


Need Your Help

is there any way to draw canvas images with relative positioning

javascript html5 canvas

I was trying to have a wheel on a canvas with multiple equal size segments similar to wheel of fortune. On the circumference of the wheel, i.e arc of each segment, I want to have an image attached ...

How to control the width of select tag?

html css

I have a list of countries, some with a very long name: