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!
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:
Just make sure the Android code is generating the same thing.
EDIT:
The code for manually generating JSON in Java would look something like:
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.