You are asking Jackson to parse a StudentList. Tell it to parse a List (of students) instead. Since List is generic you will typically use a TypeReference
List<Student> participantJsonList = mapper.readValue(jsonString, new TypeReference<List<Student>>(){});
Answer from Manos Nikolaidis on Stack OverflowYou are asking Jackson to parse a StudentList. Tell it to parse a List (of students) instead. Since List is generic you will typically use a TypeReference
List<Student> participantJsonList = mapper.readValue(jsonString, new TypeReference<List<Student>>(){});
For any one who looks for answer yet:
1.Add jackson-databind library to your build tools like Gradle or Maven
2.in your Code:
ObjectMapper mapper = new ObjectMapper();
List<Student> studentList = new ArrayList<>();
studentList = Arrays.asList(mapper.readValue(jsonStringArray, Student[].class));
android - Convert Json Array to normal Java list - Stack Overflow
Converting a JSON string into an object with a list of objects inside it - Java - Stack Overflow
How to turn json objects into an array of json objects using Java?
java - How to convert JSON file to List - Stack Overflow
Videos
ArrayList<String> list = new ArrayList<String>();
JSONArray jsonArray = (JSONArray)jsonObject;
if (jsonArray != null) {
int len = jsonArray.length();
for (int i=0;i<len;i++){
list.add(jsonArray.get(i).toString());
}
}
If you don't already have a JSONArray object, call
JSONArray jsonArray = new JSONArray(jsonArrayString);
Then simply loop through that, building your own array. This code assumes it's an array of strings, it shouldn't be hard to modify to suit your particular array structure.
List<String> list = new ArrayList<String>();
for (int i=0; i<jsonArray.length(); i++) {
list.add( jsonArray.getString(i) );
}
You have to create another class say Output as
import java.util.List;
public class Output {
public List<Token> getTokens() {
return tokens;
}
public void setTokens(List<Token> tokens) {
this.tokens = tokens;
}
private List<Token> tokens;
}
and then use
Output output = new Gson().fromJson(json, Output.class);
then you can use output to get list of tokens and go further for suggestion etc
You can use Jackson's TypeReference to achieve this, e.g.:
ObjectMapper objectMapper = new ObjectMapper();
TypeReference<List<Token>> typeReference = new TypeReference<List<Token>>() {};
List<Token> tokens = objectMapper.readValue("<json_stribg>", typeReference);
You can read more about TypeReference here.
Hi. I'm new to java and stuck wondering how I would go about turning something like this: {} {} {} into this: [{},{},{}]. I'm trying to figure out how to do this to my incoming json data using Java. The data has like 20 similar objects, with the same keys but with constantly changing values.
Any help is much appreciated.
Sincerely,
your friendly neighborhood noob
You can use the below methods to read from a file and convert the JSON array to a List of Questions.
public String readJsonFromFile(String filePath) throws IOException, ParseException {
String json = Files.readString(Paths.get("file path"));
return new JSONParser().parse(json).toString();
}
public List<Question> convert(String JsonString) throws JsonMappingException, JsonProcessingException {
ObjectMapper om = new ObjectMapper();
CollectionType typeReference =
TypeFactory.defaultInstance().constructCollectionType(List.class, Question.class);
List<Question> questions = om.readValue(JsonString, typeReference);
return questions;
}
If your JSON keys and Java class fields are different, use @JsonPropery annotation to map keys to fields
import com.fasterxml.jackson.annotation.JsonProperty;
public class Question {
@JsonProperty("question")
private String title;
@JsonProperty("answer 1")
private String a1;
@JsonProperty("answer 2")
private String a2;
@JsonProperty("answer 3")
private String a3;
@JsonProperty("correct answer")
private String cA;
//setters & getters
}
Required Jars:
jackson-annotations.jar
jackson-core.jar
jackson-databind.jar
json-simple.jar //used to parse Json file to String
You have two steps
Get JSON string from the file, use read file from assets
Extract items from the JSON string use How do I convert a JSON array into a Java List. I'm using svenson