我在这里的第一篇文章:)
我有一份暑期工作要做一些编码 - 我绝对不是专业人士,但!
我正在将此 iPhone 应用程序转换为 Android,它几乎完成了。只是在努力转换这件作品。
“参与者”数组通过 JSON 传递到服务器。对我来说,在 Android 中,它看起来像是一个 HashMap 数组。那是对的吗?
这是目标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];
}
getListOfGameParticipants 是:
- (NSDictionary*) getListOfGameParticipants
{
return participants ;
}
我尝试过的Android代码是:
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);
请帮忙 :)
编辑:它不起作用。服务器报告:
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.
预期的数据类型是:
[DataMember]
public Player[] ParticipantIds { get; set; }
编辑:进一步更新:
我现在将我的哈希图数组解析为格式良好的 JSON 字符串。我需要将该 JSON 字符串解析为 JSONObject 并将该 JSONObject 放入 JSONArray。
以下代码不起作用:
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);
它给出了错误:
org.json.JSONException: No value for [{"LastName":"Test Surname","FirstName":"Test Firstname","Id":"test@test.net","PhoneNumber":"TestNumber"}]
帮助!