这是一个json文件。
{
"RackItems": [
{
"Name": "Profile",
"Description": "Profile Items",
"MainContentItems": [
{
"Name": "Personal",
"Description": "Profile",
"ContentItems": [
{
"Name": "Personal Details",
"Type": "Profile"
}
]
},
{
"Name": "My Playlists",
"Description": "Playlists",
"ContentItems": [
{
"Name": "My Playlists",
"Description": "My Playlists",
"Type": "Playlist"
}
]
}
]
},
{
"Name": "Home",
"Description": "Home Items",
"ContentItems": [
{
"Name": "Top Songs",
"Description": "Top Songs",
"Type": "Song"
},
{
"Name": "Top Albums",
"Description": "Top Albums",
"Type": "Album"
}
]
}
],
"DetailedItems": {
"Genre": {
"Name": "Genre",
"Description": "Genre Items",
"ContentItems": [
{
"Name": "Top @@ Songs",
"Description": "Top Songs"
},
{
"Name": "Top @@ Albums",
"Description": "Top Albums"
}
]
},
"UserProfile": {
"Name": "User Profile",
"Description": "User Profile",
"MainContentItems": [
{
"Name": "Personal",
"Description": "Profile",
"ContentItems": [
{
"Name": "Personal Details",
"Description": "Personal Details",
"ItemType": "ProfileView"
}
]
},
{
"Name": "Playlists",
"Description": "Playlists",
"ContentItems": [
{
"Name": "Playlists",
"Description": "Playlists",
"ItemType": "Playlist"
}
]
}
]
}
},
"DownloadSongs": {
"Name": "Download Songs",
"Description": "Download Songs",
"ItemType": "DownloadSongsResult"
}
}
这是个别的pojo类:
@JsonIgnoreProperties(ignoreUnknown = true)
public class ContentMusicPlayerChild {
String Name, Description;
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getDescription() {
return Description;
}
public void setDescription(String description) {
Description = description;
}
}
和其他具有相同类型的getter setter和顶级的pojo
List<Object> cStringList = new ArrayList<>();
@JsonAnyGetter
public List<Object> getcStringList() {
return cStringList;
}
@JsonAnySetter
public void setcStringList(List<Object> cStringList) {
this.cStringList = cStringList;
}
在这里,我使用了JsonAnyGetter和JsonAnySetter来获取包含的任何数据。喜欢
ContentMusicPlayer contentMusicPlayer = null;
ObjectMapper objectMapper = new ObjectMapper();
contentMusicPlayer = objectMapper.readValue(UtilMethod.loadJSONFromAsset(context), ContentMusicPlayer.class);
Log.e("contentMusicPlayer", contentMusicPlayer+ "");
return contentMusicPlayer;
但它返回我为空。我在哪里想念。这些可以像
@JsonProperty("DetailedItems")
private DetailedItems DetailedItems;
@JsonProperty("SimilarArtists")
private SimilarArtists SimilarArtists;
@JsonProperty("RackItems")
private List<RackItems> RackItems;
但是我不想使硬代码成为可能,但是将来可以更改。
最佳答案
//Main parser class
public class MainParser {
RackItemsData RackItems;
GenereData DetailedItems;
public DownloadSongsData getDownloadSongs() {
return DownloadSongs;
}
public void setDownloadSongs(DownloadSongsData downloadSongs) {
DownloadSongs = downloadSongs;
}
public GenereData getDetailedItems() {
return DetailedItems;
}
public void setDetailedItems(GenereData detailedItems) {
DetailedItems = detailedItems;
}
public RackItemsData getRackItems() {
return RackItems;
}
public void setRackItems(RackItemsData rackItems) {
RackItems = rackItems;
}
DownloadSongsData DownloadSongs;
}
// Second class
public class RackItemsData {
String Name;
String Description;
ArrayList<MainContentItemsData> MainContentItems;
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getDescription() {
return Description;
}
public void setDescription(String description) {
Description = description;
}
public ArrayList<MainContentItemsData> getMainContentItems() {
return MainContentItems;
}
public void setMainContentItems(ArrayList<MainContentItemsData> mainContentItems) {
MainContentItems = mainContentItems;
}
}
//ThirdClass
public class MainContentItemsData {
String Name;
String Description;
ArrayList<ContentItemsData> ContentItems;
public ArrayList<ContentItemsData> getContentItems() {
return ContentItems;
}
public void setContentItems(ArrayList<ContentItemsData> contentItems) {
ContentItems = contentItems;
}
public String getDescription() {
return Description;
}
public void setDescription(String description) {
Description = description;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
}
//Forth class
public class ContentItemsData {
String Name;
String Description;
String Type;
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getDescription() {
return Description;
}
public void setDescription(String description) {
Description = description;
}
public String getType() {
return Type;
}
public void setType(String type) {
Type = type;
}
}
//Fifth Class
public class GenereData {
MainContentItemsData Genre;
public MainContentItemsData getGenre() {
return Genre;
}
public void setGenre(MainContentItemsData genre) {
Genre = genre;
}
public RackItemsData getUserProfile() {
return UserProfile;
}
public void setUserProfile(RackItemsData userProfile) {
UserProfile = userProfile;
}
RackItemsData UserProfile;
}
//Sixth class
public class DownloadSongsData {
String Name;
String Description;
String ItemType;
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getDescription() {
return Description;
}
public void setDescription(String description) {
Description = description;
}
public String getItemType() {
return ItemType;
}
public void setItemType(String itemType) {
ItemType = itemType;
}
}
/////
use GSon libraru and parse data;
Gson gson = new Gson();
MainParser resultObject = gson.fromJson(jsonResultString, MainParser.class);
关于java - 如何在Android Java中使用fasterxml jackson动态解析本地Json以获取固定的pojo类,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36029464/