0

我在这里的第一篇文章:)

我有一份暑期工作要做一些编码 - 我绝对不是专业人士,但!

我正在将此 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"}]

帮助!

4

1 回答 1

0

Android 代码与 iPhone 代码做的事情不同,但就数据结构而言,它似乎是正确的。只需比较这两种方法生成的 JSON,就知道哪里出了问题。

这是 iPhone 生成的 JSON 代码:

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

只需确保 Android 代码生成相同的内容即可。

编辑:

在 Java 中手动生成 JSON 的代码如下所示:

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) + "]";

我不经常编写 Java 代码,但我认为上面生成的 JSON 与您发布的 Objective-C 代码相同。它没有缩进,但我认为服务器不会关心。

于 2012-06-15T22:04:05.797 回答