Android Jackson创建JSON但生成无效结果。

huangapple go评论72阅读模式
英文:

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"
      }
    ]
  }
]

huangapple
  • 本文由 发表于 2023年6月19日 07:18:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76502850.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定