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));
Videos
For anyone else who might need this:
String jsonString = "[\"string1\",\"string2\",\"string3\"]";
ObjectMapper mapper = new ObjectMapper();
List<String> strings = mapper.readValue(jsonString, List.class);
As ryzhman said, you are able to cast it to a List, but only of the object (JSONArray in ryzhman's case) extends the ArrayList class. You don't need an entire method for this. You can simply:
List<String> listOfStrings = new JSONArray(data);
Or if you are using IBM's JSONArray (com.ibm.json.java.JSONArray):
List<String> listOfStrings = (JSONArray) jsonObject.get("key");
Your JSON matches an object (see HotelPromotionList below), not a list.
class HotelPromotionList {
List<HotelPromotion> hotelPromotions;
}
class HotelPromotion {
int id;
int version;
String description;
String status; // might be an enum
// ...
}
This works for me, use it for different types, hope this will help
public class RestMapper
{
private ObjectMapper objectMapper;
public <T> List<T> stringJsonToObject( String json, Class<T> objectType )
{
List<T> resultList = new LinkedList<T>();
ListOfObjects<T> arrObject;
try
{
arrObject = objectMapper.readValue( json, ListOfObjects.class );
for ( int i = 0; i < arrObject.getBody().size(); i++ )
{
T singleObject = objectMapper.convertValue( arrObject.getBody().get( i ), objectType );
resultList.add( singleObject );
}
}
catch ( IOException e )
{
e.printStackTrace();
}
return resultList;
}
static class ListOfObjects<T>
{
private List<T> body;
public ListOfObjects()
{
}
public void setBody( List<T> body )
{
this.body = body;
}
public List<T> getBody()
{
return body;
}
}
You will need to pass a TypeReference to readValue with the desired result type:
ObjectMapper mapper = new ObjectMapper();
List<Map<String, Object>> data = mapper.readValue(json, new TypeReference<List<Map<String, Object>>>(){});
Use gson with specified type to convert to list of maps:
Gson gson = new Gson();
Type resultType = new TypeToken<List<Map<String, Object>>>(){}.getType();
List<Map<String, Object>> result = gson.fromJson(json, resultType);
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.
Your input string should be as follows to qualify for a list of SomeObject
{
[{
"k1": 1,
"k2": "v2",
"k3": "v3"
}, {
"k1": 2,
"k2": "v2",
"k3": "v3"
}
]
}
Below code should work ..
JSONObject jsonObject = new JSONObject(jsonString);
SomeObject[] objectsList = gson.fromJson(jsonObject.toString(), SomeObject[].class);
For given JSON String here you can handle this way (add toString() to SomeObject for displaying it)...
import java.util.LinkedList;
import java.util.List;
import java.util.Map.Entry;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
public class TestClass {
public static void main(String[] args) {
List<SomeObject> list = new LinkedList<SomeObject>();
String jsonString = "{\"obj_1\":{\"k1\":1,\"k2\":\"v2\",\"k3\":\"v3\"},\"obj_2\":{\"k1\":2,\"k2\":\"v2\",\"k3\":\"v3\"}}";
Gson gson = new Gson();
JsonObject jsonObject = new JsonParser().parse(jsonString).getAsJsonObject();
for (Entry<String, JsonElement> entry : jsonObject.entrySet()) {
// note entry.getKey() will be obj_1, obj_2, etc.
SomeObject item = gson.fromJson(entry.getValue().getAsJsonObject(), SomeObject.class);
list.add(item);
}
System.out.println(list);
}
}
Sample Run
[SomeObject [k1=1, k2=v2, k3=v3], SomeObject [k1=2, k2=v2, k3=v3]]
NOTE: the value of k1 must be a number and without quotes as k1 is declared as int in SomeObject
Your JSON structure is a Map, if you want an array or list use [ { } , {} ] it is the structure in JSON for array or list
This way is worked for me:
Convert all list to String.
String element = gson.toJson(
groupsList,
new TypeToken<ArrayList<GroupItem>>() {}.getType());
Create JSONArray from String:
JSONArray list = new JSONArray(element);
The org.json based classes included in Android don't have any features related to converting Java POJOs to/from JSON.
If you have a list of some class (List<GroupItem>) and you absolutely need to convert that to a org.json.JSONArray you have two choices:
A) Use Gson or Jackson to convert to JSON, then parse that JSON into a JSONArray:
List<GroupItem> list = ...
String json = new Gson().toJson(list);
JSONArray array = new JSONArray(json);
B) Write code to create the JSONArray and the JSONObjects it will contain from your Java objects:
JSONArray array = new JSONArray();
for (GroupItem gi : list)
{
JSONObject obj = new JSONObject();
obj.put("fieldName", gi.fieldName);
obj.put("fieldName2", gi.fieldName2);
array.put(obj);
}
Your root JSON is an Array, so first create a JSONArray from your String.
Do this:
JSONArray arr = new JSONArray(jstring);
for (int i = 0; i < arr.length(); i++) { // Walk through the Array.
JSONObject obj = arr.getJSONObject(i);
JSONArray arr2 = obj.getJSONArray("fileName");
// Do whatever.
}
For more info, please refer to the docs on JSONArray and JSONObject.
You have to directly construct JSONArray from JSON string in this case.
JSONArray arr = new JSONArray(jstring);