Reputation: 437
That is an example input that I should convert to pojo.
[{name=status, value=OK}, {name=desc, value=missing}, {name=lat, value=51.247049}, {name=lon, value=22.017532}]
That is pojo
public class Params {
private String name;
private String value;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
Since this input is not valid, I can not parse it to pojo. How can I parse that json string to pojo?
Not: I can not fix input, thus that is not an option.
Upvotes: 0
Views: 303
Reputation: 152
How to parse an invalid json to pojo? This input is not even a json, so you shouldn't be thinking in terms of parsing it into a Java Object using Jackson. Like Alex Hart mentioned above, you could use regex to parse this String, or write your own logic (looping and use String utility methods to extract names and values). Converting this input into a json first, and then parsing adds redundancy.
Upvotes: 0
Reputation: 1673
Converting things to JSON first seems like an unnecessary extra step.
Given you've got Params set up to take two strings, writing a Regular Expression to parse your input is very straightforward, as long as the data input format is consistent:
\{name=([a-zA-Z0-9.]+), value=([a-zA-Z0-9.]+)\}
This will give you a set of groups, each with 3 indicies, the entire match:
{name=status, value=OK}
Just the name:
status
and just the value:
OK
You can then iterate over this set of groups and build out your Param objects. If you still need a JSON representation of these at some point, you should quite easily be able to transform a Param POJO into a JSON value.
Upvotes: 1