0

我有一个字符串,我在提交表单后从 UI 获得。它从 UI 中存在的动态表中读取,然后提交后,它正在创建如下所示的数据列表。

[{"keyname0":"url","keyvalue0":" http://www.google.com "},{"keyname1":"year","keyvalue1":"2013"},{"keyname2": "bill","keyvalue2":"senate"},{"keyname3":"type","keyvalue3":"html"}]

我在数据库中有两列。一个是“KEYNAME”,另一个是“KEYVALUE”。我想将 KEYNAME 和 KEYVALUE 的每个值插入数据库。

有人可以帮忙吗?

4

1 回答 1

0

使用杰克逊:

private static final KEYNAME = "keyname";
private static final KEYVALUE = "keyvalue";

final ObjectMapper mapper = new ObjectMapper();

final JsonNode data = mapper.readTree(theString);

String key, value;
Iterator<Map.Entry<String, JsonNode>> members;
Map.Entry<String, JsonNode> member;

for (final JsonNode element: data) {
    key = null; value = null;
    members = element.fields();
    while (members.hasNext()) {
        member = members.next();
        if (member.getKey().startsWith(KEYNAME))
            key = member.getValue().textValue();
        if (member.getKey().startsWith(KEYVALUE))
            value = member.getValue().textValue();
    }
    // check if key or value are null, if yes problem!
    // process key and value
}
于 2013-06-07T19:51:43.733 回答