我使用的是API,我试图用Jackson反序列化结果,但我总是犯同样的错误。
我猜我的POJO不对,但我找不到错误。
错误在于libs
我试图声明为String或List而没有任何成功。有什么想法吗?
{
"libs": {},
"items": [
{
"id": "001",
"cars": [
"cd1042af-856d-4649-a170-032d15a4119b",
"00ed61a4-3aab-4722-90c2-7f4cca4cbded",
"8fa3aa8b-3f22-4413-a41d-e78de9051de2"
],
"name": "James"
},
{
"id": "002",
"cars": [
"cd1043af-856d-4649-a170-032d15a4119b",
"00ed61a4-3aab-4722-90c2-7f4cca4cbded",
"8fa3aa8b-3f22-4413-a41d-e78de9051de2"
],
"name": "James"
}]
}
public class Page<Car> {
private List<String> libs;
private List<Car>items;
public List<Car> getItems() {
return items;
}
public void setItems(List<Car> items) {
this.items = items;
}
public List<String> getLibs() {
return libs;
}
public void setLibs(List<String> libs) {
this.libs = libs;
}
}
答案 0 :(得分:2)
您将libs声明为列表,但使用了maps / objects括号。
应为"libs": []
在name
属性在每个列表元素中结束后,您有额外的逗号。
答案 1 :(得分:1)
更正的json如下:
// Server setup from node.js website
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World\n');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
// Trying to listen for data from React app to feed into JSON (broken)
var express = require("express");
var myParser = require("body-parser");
var app = express();
app.use(myParser.urlencoded({extended : true}));
app.post("/scene-setup.json", function(request, response) {
console.log(request.body); //This prints the JSON document received (if it is a JSON document)
});
app.listen(3001);
// Updating JSON file with "obj" (working)
var jsonfile = require('jsonfile')
var file = './scene-setup.json'
var obj = {name: 'JP'}
jsonfile.writeFile(file, obj, function (err) {
console.error(err)
})
答案 2 :(得分:0)
使用Jackson库解决了此问题,这是我的代码段。
**Main Class:**
public class MainClass {
public static void main(String[] args) throws JsonParseException,
JsonMappingException, IOException {
String jsonStr = "{\r\n" + " \"libs\": [\"ram\", \"shyam\"],\r\n" + " \"items\": [{\r\n"
+ " \"id\": \"001\",\r\n" + " \"cars\": [\r\n"
+ " \"cd1042af-856d-4649-a170-032d15a4119b\",\r\n"
+ " \"00ed61a4-3aab-4722-90c2-7f4cca4cbded\",\r\n"
+ " \"8fa3aa8b-3f22-4413-a41d-e78de9051de2\"\r\n" + " ],\r\n"
+ " \"name\": \"James\"\r\n" + " },\r\n" + " {\r\n"
+ " \"id\": \"002\",\r\n" + " \"cars\": [\r\n"
+ " \"cd1043af-856d-4649-a170-032d15a4119b\",\r\n"
+ " \"00ed61a4-3aab-4722-90c2-7f4cca4cbded\",\r\n"
+ " \"8fa3aa8b-3f22-4413-a41d-e78de9051de2\"\r\n" + " ],\r\n"
+ " \"name\": \"James\"\r\n" + " }\r\n" + " ]\r\n" + "}";
ObjectMapper mapper = new ObjectMapper();
MyPojo details = mapper.readValue(jsonStr, MyPojo.class);
for (String itrs : details.getLibs()) {
System.out.println("Value for getLibs is: " + itrs);
}
for (Items itr : details.getItems()) {
System.out.println("Value for getId is: " + itr.getId());
System.out.println("Value for getName is: " + itr.getName() + "\n");
for (String carItr : itr.getCars()) {
System.out.println("Value for getCars is: " + carItr);
} } }}
**MyPojo**
import java.util.ArrayList;
public class MyPojo {
private ArrayList<Items> items;
private String[] libs;
public ArrayList<Items> getItems() {
return items;
}
public void setItems(ArrayList<Items> items) {
this.items = items;
}
public String[] getLibs() {
return libs;
}
public void setLibs(String[] libs) {
this.libs = libs;
} }
**Items**
public class Items {
private String id;
private String[] cars;
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String[] getCars() {
return cars;
}
public void setCars(String[] cars) {
this.cars = cars;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
} }
**RESULTS:**
Value for getLibs is: ram
Value for getLibs is: shyam
Value for getId is: 001
Value for getName is: James
Value for getCars is: cd1042af-856d-4649-a170-032d15a4119b
Value for getCars is: 00ed61a4-3aab-4722-90c2-7f4cca4cbded
Value for getCars is: 8fa3aa8b-3f22-4413-a41d-e78de9051de2
Value for getId is: 002
Value for getName is: James
Value for getCars is: cd1043af-856d-4649-a170-032d15a4119b
Value for getCars is: 00ed61a4-3aab-4722-90c2-7f4cca4cbded
Value for getCars is: 8fa3aa8b-3f22-4413-a41d-e78de9051de2
**注意:我已经修改了库以添加一些字符串值,以查看我们的状况。