简体   繁体   中英

Fetch table data from a string and then insert into db

I have a string which i am getting from UI after submitting a form. Its reading from a dynamic table present in the UI and then post submitting, it is creating a list of data like below.

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

i have two column in database. one is "KEYNAME" and other one is "KEYVALUE". I want insert each value of KEYNAME and KEYVALUE into database.

Can anyone please help?

Using Jackson:

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM