使用List和Hashmap创建Java Json数组对象时输出错误

时间:2016-07-09 08:06:45

标签: java json hashmap

我想用List和hashmap创建一个Json数组对象,但是在程序执行期间我遇到了一些奇怪的问题。

这是我的代码:

List<HashMap> list = new ArrayList<>();
HashMap<String, String> hmap = new HashMap<>();
String raw = "<CONTENT><DETAIL><USER>user1</USER><MAIL>abc@test.com</MAIL></DETAIL><DETAIL><USER>user2</USER><MAIL>def@test.com</MAIL></DETAIL></CONTENT>";
String partRas = "";
int index = raw.indexof("<USER>");

while(index >= 0) {
    if(index + 50 < raw.length())
        partRaw = raw.subString(index, index + 50);

    hmap("user", getTagValue(partRaw, "USER"));
    hmap("email", getTagValue(partRaw, "MAIL"));
    list.add(hmap);

    raw = raw.subString(index + 50, raw.length());
    index = raw.indexof("<USER>");
}

System.out.println(new Gson().toJson(list));


public static String getTagValue(String xml, String tagName){
    return xml.split("<"+tagName+">")[1].split("</"+tagName+">")[0];
}

这段代码看起来合法,但是当我执行这段代码时,我应该得到这样的结果:

[{"user":"user1","email":"abc@test.com"},{"user":"user2","email":"def@test.com"}]

但我得到的是:

[{"user":"user2","email":"def@test.com"},{"user":"user2","email":"def@test.com"}]

我能想到的这个问题的唯一答案是&#34; 它是PASS BY ADDRESS &#34;,所以推送到List的第一个结果受到第二次迭代的影响。

我想问的问题:有谁知道如何获得我想要的结果?

我不希望答案告诉我在开始时定义多个HashMaps,因为我的原始数据可能包含更多&#34;&#34;这意味着它可以包含尽可能多的&#34; USER&#34;和&#34; MAIL&#34;数据尽可能。

1 个答案:

答案 0 :(得分:0)

hmap在您的代码中被重用,值被覆盖,因此当您将其添加到列表中时,您只是添加了对同一事物的新引用。

List<HashMap> list = new ArrayList<>();
HashMap<String, String> hmap;
String raw = "<CONTENT><DETAIL><USER>user1</USER><MAIL>abc@test.com</MAIL></DETAIL><DETAIL><USER>user2</USER><MAIL>def@test.com</MAIL></DETAIL></CONTENT>";
String partRas = "";
int index = raw.indexof("<USER>");

while(index >= 0) {
    if(index + 50 < raw.length())
        partRaw = raw.subString(index, index + 50);

    /*
     * Create a new, empty HashMap 
     */
    hmap = new HashMap<>();
    hmap("user", getTagValue(partRaw, "USER"));
    hmap("email", getTagValue(partRaw, "MAIL"));
    list.add(hmap);

    raw = raw.subString(index + 50, raw.length());
    index = raw.indexof("<USER>");
}

System.out.println(new Gson().toJson(list));

public static String getTagValue(String xml, String tagName){
    return xml.split("<"+tagName+">")[1].split("</"+tagName+">")[0];
}

你可能真的想要检查一下(即使错误检查很糟糕):

String raw = "<CONTENT><DETAIL><USER>user1</USER><MAIL>abc@test.com</MAIL></DETAIL><DETAIL><USER>user2</USER><MAIL>def@test.com</MAIL></DETAIL></CONTENT>";
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder;

try {
    dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(new ByteArrayInputStream(raw.getBytes()));
    NodeList nodes = doc.getElementsByTagName("DETAIL");
    for (int idx = 0; idx < nodes.getLength(); idx++) {
        Node node = nodes.item(idx);
        System.out.println("User: "+((Element)node).getElementsByTagName("USER").item(0).getTextContent());
        System.out.println("Mail: "+((Element)node).getElementsByTagName("MAIL").item(0).getTextContent());
    }
} catch (Exception ex) {
    // do stuff
}