英文:
Android Jackson creating JSON but produces non-valid result
问题
[{
"MOVIE_CATEGORY": "#",
"videos": [{
"title": "Movie Name 1",
"videoUrl": "1.mp4",
"bgImageUrl": "2.jpg",
"cardImageUrl": "3.png"
}, {
"title": "Movie Name 2",
"videoUrl": "2.mp4",
"bgImageUrl": "2.jpg",
"cardImageUrl": "3.png"
}, {
// ...
}]
}, {
"MOVIE_CATEGORY": "A",
"videos": [{
"title": "Movie Name 4",
"videoUrl": "3.mp4",
"bgImageUrl": "2.jpg",
"cardImageUrl": "3.png"
}, {
"title": "Movie Name 5",
"videoUrl": "4.mp4",
"bgImageUrl": "2.jpg",
"cardImageUrl": "3.png"
}, {
// ...
}]
}, {
"MOVIE_CATEGORY": "B",
"videos": [{
// ...
}]
}]
英文:
Hey all I am trying to create the json below using Jackson for android java:
[{
"MOVIE_CATEGORY": "#",
"videos": [{
"title": "Movie Name 1",
"videoUrl": "1.mp4",
"bgImageUrl": "2.jpg",
"cardImageUrl": "3.png"
}, {
"title": "Movie Name 2",
"videoUrl": "2.mp4",
"bgImageUrl": "2.jpg",
"cardImageUrl": "3.png"
}, {
.....
}
]
}, {
"MOVIE_CATEGORY": "A",
"videos": [{
"title": "Movie Name 4",
"videoUrl": "3.mp4",
"bgImageUrl": "2.jpg",
"cardImageUrl": "3.png"
}, {
"title": "Movie Name 5",
"videoUrl": "4.mp4",
"bgImageUrl": "2.jpg",
"cardImageUrl": "3.png"
}, {
....
}
]
}, {
"MOVIE_CATEGORY": "B",
"videos": [{
...
}
]
}
]
The code here:
for (int i = 0; i < fileList.length; ++i) {
if (!fileList[i].contains("_backdrop.")) {
ObjectMapper mapper = new ObjectMapper();
ObjectNode rootNode = mapper.createObjectNode();
rootNode.put("MOVIE_CATEGORY","#");
ObjectNode childNode1 = mapper.createObjectNode();
childNode1.put("title", "Movie 1");
childNode1.put("videoUrl", "1.mp4");
childNode1.put("bgImageUrl", "2.jpg");
childNode1.put("cardImageUrl", "3.png");
rootNode.set("videos", childNode1);
String jsonString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(rootNode);
Log.d("", jsonString);
}
}
Produces only this:
{
"MOVIE_CATEGORY" : "#",
"videos" : {
"title" : "Movie 1",
"videoUrl" : "1.mp4",
"bgImageUrl" : "2.jpg",
"cardImageUrl" : "3.png"
}
}
Which in itself is not correct JSON. How can I get this to look like my JSON example structure?
答案1
得分: 1
[
{
"MOVIE_CATEGORY": "#1",
"videos": [
{
"title": "Movie 1"
},
{
"title": "Movie 2"
}
]
},
{
"MOVIE_CATEGORY": "#2",
"videos": [
{
"title": "Movie 1"
},
{
"title": "Movie 2"
}
]
}
]
英文:
I hope I have understood your question correctly, that it is about the complete JSON.
[]
is an array and {}
is an object. Therefore you must use createArrayNode()
or createObjectNode()
.
ObjectMapper mapper = new ObjectMapper();
// Create array for categories
ArrayNode categories = mapper.createArrayNode();
for (int i = 1; i <= 2; i++) {
// Create object category
ObjectNode category = mapper.createObjectNode();
category.put("MOVIE_CATEGORY","#" + i);
// Create array for videos
ArrayNode videos = mapper.createArrayNode();
for (int j = 1; j <= 2; j++) {
// Create object for movie
ObjectNode movie = mapper.createObjectNode();
movie.put("title", "Movie " + j);
// Add object movie to array videos
videos.add(movie);
}
// Put videos to object category
category.put("videos", videos);
// Add category to array categories
categories.add(category);
}
String jsonString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(categories);
Output
[
{
"MOVIE_CATEGORY":"#1",
"videos":[
{
"title":"Movie 1"
},
{
"title":"Movie 2"
}
]
},
{
"MOVIE_CATEGORY":"#2",
"videos":[
{
"title":"Movie 1"
},
{
"title":"Movie 2"
}
]
}
]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论