How to convert the following json string to java object?

Rolando :

I want to convert the following JSON string to a java object:

String jsonString = "{
"libraryname":"My Library",
"mymusic":[{"Artist Name":"Aaron","Song Name":"Beautiful"},
{"Artist Name":"Britney","Song Name":"Oops I did It Again"},
{"Artist Name":"Britney","Song Name":"Stronger"}]}"

My goal is to access it easily something like:

(e.g. MyJsonObject myobj = new MyJsonObject(jsonString)
myobj.mymusic[0].id would give me the ID, myobj.libraryname gives me "My Library").

I've heard of Jackson, but I am unsure how to use it to fit the json string I have since its not just key value pairs due to the "mymusic" list involved. How can I accomplish this with Jackson or is there some easier way I can accomplish this if Jackson is not the best for this?

StaxMan :

No need to go with GSON for this; Jackson can do either plain Maps/Lists:

ObjectMapper mapper = new ObjectMapper();
Map<String,Object> map = mapper.readValue(json, Map.class);

or more convenient JSON Tree:

JsonNode rootNode = mapper.readTree(json);

By the way, there is no reason why you could not actually create Java classes and do it (IMO) more conveniently:

public class Library {
  @JsonProperty("libraryname")
  public String name;

  @JsonProperty("mymusic")
  public List<Song> songs;
}
public class Song {
  @JsonProperty("Artist Name") public String artistName;
  @JsonProperty("Song Name") public String songName;
}

Library lib = mapper.readValue(jsonString, Library.class);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to convert JSON string into List of Java object?

How to convert the following JSON String to POJO

How to convert Java String to JSON Object

Convert a JSON string to object in Java ME?

How to convert JSON to Java object?

how to convert object to string in java

How to convert a java object to simple json string without converting the LocalDateTime field to an extended json object?

How to convert the following string to date or calendar object in Java?

How to convert the Object[] to String[] in Java?

How to convert Java object to JSON

How should my DTO class look like if I have to convert the following JSON into Java Object?

How to convert String containing JSON object to actual JSON using Java

How to convert this nested json string to a java object?

How to convert the following JSON array into IDictionary<string, object>?

How to convert JSON String to JSON Object in swift?

How will i convert json string to Json Object

How to convert an XML structured-like object to a JSON string in Java?

Spark convert JSON string to JSON object (Java)

How to convert the following string to json object?

How to convert the following JSON string into a dictionary C#

how to convert JSON string to object in JAVA android

How to Convert String Array JSON in a Java Object

How to convert the following odd string into json in python?

Java string convert to JSON object for http POST

How to convert the following data from a DataTable to a JSON object

How to convert string to json string and object in java

How to convert a java object into a Json

How to convert String to Json Object in java or groovy

How to convert to XMl string to java object or json object