0

I'm working on a game and I'm implementing objects where you can define in "TiledMap editor" what items a certain object holds. So I've got to an idea where I can enter the Item ID's right there like {22:4, id:amount}. When I parse the map, I retrieve that array as a string, is there a way to convert it to an array?

Thanks in advance!

4

1 回答 1

0

首先,您可能想要 a Map,而不是 aarray或 a List

Map<String,String> processParams(String list) {
    Map<String,String> = new HashMap<String,String>();
    int openBracket = list.indexOf("{");
    int closeBracket = list.lastIndexOf("}");
    String params = list.substring(openBracket+1,closeBracket);
    String paramList = params.split(",");

    for(String param: paramList) {
       String pData = param.trim().split(":");
       map.put(param[0].trim(),param[1].trim());
    }
    return map;
}

processParams("{22:4, id:amount}");

当然,它实际上是一个类似 JSON 的结构,所以可能已经存在解析器。

于 2013-08-25T00:01:26.533 回答