我想使用jackson-databind将json反序列化为一个包含其他对象列表的对象。
我正在尝试在json下面进行反序列化。我有class class1,它有一个List<NestedProp> prop2
。 Prop2是一个arraylist。 class Class1 {
String prop1;
List<NestedProp> prop2;
//setters getters
}
class NestedProp {
String p1;
String p2;
//setters getters
}
`
```
"class1": {
"prop1": "pp",
"prop2": [
{
"nestedProp": [
{
"p1": "127",
"p2": "1"
},
{
"p1": "128",
"p2": "2"
}
]
}
]
}
。
经典结构 `
While deserialisingI get the below exception:
>com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `NestedProp` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('127')
at [Source: (File); line: 59, column: 7] (through reference chain: java.util.ArrayList[1]->Class1["prop2"]->java.util.ArrayList[0])
List<Class1> testData = objectMapper.readValue(test.json, Class1.class);
class RBC
def initialize
@args = ["Create a new card"]
@functions = ["create_rbc"]
puts "Do you have an RBC ID yet? Yes(0) No(1)"
hasrbc = gets.chomp.to_i
if hasrbc == 1
@balance = 5
create_rbc
else
login
end
end
def create_rbc
puts "\nGenerating your rbc\n\n"
puts "\nWelcome to your Ruby Binary Card(RBC)!\n\n"
puts "Your RBC will keep track of your RubyCredits(RC).\n"
puts "You will get paid RC for work apps, and pay for game apps.\n"
puts "If you lose track of your RBC ID, you can get a new one.\n"
puts "Doing this, however, will reset your balance to the default of $5\n\n"
puts "What is your name? Do first last\n"
@fullname = gets.chomp
@card_name = get_name_codec(@fullname)
@card_cipher = "#{rand(1..9)}#{rand(1..9)}#{rand(1..9)}#{rand(1..9)}#{rand(1..9)}#{rand(1..9)}#{rand(1..9)}#{rand(1..9)}#{rand(1..9)}#{rand(1..9)}"
@card_id = "#{@card_name} - #{@card_cipher}"
instance_variable_set("@Id#{@card_cipher}", @balance)
puts "Write down your RBC ID: #{@card_id}"
file = File.open("Cards.rbc", "w")
file.puts @card_id
file.puts @balance
end
end
def get_name_codec(name)
names = name.split(" ")
fname = names[0]
lname = names[1]
fchar = fname.split(//)
fcodec = "#{fchar[0]}#{fchar[1]}"
name_codec = "#{fcodec}#{lname}"
return name_codec
end
def login
@found = false
puts "What is your RBC Id"
input = gets.chomp
File.open("Cards.RBC", "r") do |f|
f.each_line do |line|
if input == "#{line}"
@card_id = line.to_s
@found == true
elsif @found == true
@balance = line.to_i
end
end
end
puts "#{@card_id}#{@balance}"
end
RBC.new
答案 0 :(得分:0)
JSON
有效负载中有两个数组。因此,您需要为其创建额外的POJO
。参见以下示例:
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.File;
import java.util.List;
public class JsonApp {
public static void main(String[] args) throws Exception {
File jsonFile = new File("./resource/test.json").getAbsoluteFile();
ObjectMapper mapper = new ObjectMapper();
Root root = mapper.readValue(jsonFile, Root.class);
System.out.println(root);
}
}
class Root {
private Class1 class1;
//getters, setters, toString
}
class Class1 {
private String prop1;
private List<NestedProps> prop2;
//getters, setters, toString
}
class NestedProps {
private List<NestedProp> nestedProp;
//getters, setters, toString
}
class NestedProp {
private String p1;
private String p2;
//getters, setters, toString
}
对于JSON
以下:
{
"class1": {
"prop1": "pp",
"prop2": [
{
"nestedProp": [
{
"p1": "127",
"p2": "1"
},
{
"p1": "128",
"p2": "2"
}
]
}
]
}
}
上面的示例打印:
Root{class1=Class1{prop1='pp', prop2=[NestedProps{nestedProp=[NestedProp{p1='127', p2='1'}, NestedProp{p1='128', p2='2'}]}]}}
另请参阅: