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));
Converting a JSON string into an object with a list of objects inside it - Java - Stack Overflow
json - How to convert automatically list of objects to JSONArray with Gson library in java? - Stack Overflow
How to convert list data into json in java - Stack Overflow
android - Convert JSON String to List of Java Objects - Stack Overflow
Videos
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.
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);
}
Using gson it is much simpler. Use following code snippet:
// create a new Gson instance
Gson gson = new Gson();
// convert your list to json
String jsonCartList = gson.toJson(cartList);
// print your generated json
System.out.println("jsonCartList: " + jsonCartList);
Converting back from JSON string to your Java object
// Converts JSON string into a List of Product object
Type type = new TypeToken<List<Product>>(){}.getType();
List<Product> prodList = gson.fromJson(jsonCartList, type);
// print your List<Product>
System.out.println("prodList: " + prodList);
public static List<Product> getCartList() {
JSONObject responseDetailsJson = new JSONObject();
JSONArray jsonArray = new JSONArray();
List<Product> cartList = new Vector<Product>(cartMap.keySet().size());
for(Product p : cartMap.keySet()) {
cartList.add(p);
JSONObject formDetailsJson = new JSONObject();
formDetailsJson.put("id", "1");
formDetailsJson.put("name", "name1");
jsonArray.add(formDetailsJson);
}
responseDetailsJson.put("forms", jsonArray);//Here you can see the data in json format
return cartList;
}
you can get the data in the following form
{
"forms": [
{ "id": "1", "name": "name1" },
{ "id": "2", "name": "name2" }
]
}
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
With Jackson you would do something like:
String jsonStr = "...";
ObjectReader reader = new ObjectMapper().reader();
List<Map<String, Object>> objs = reader.readValue(jsonStr);
Map<String, Object> myFirstObj = objs.get(0);
myFirstObj.get("data");
myFirstObj.get("what");
// and so on
Or even better you can create a pojo and do:
String jsonStr = "...";
ObjectMapper mapper = new ObjectMapper();
List<MyPojo> myObjs = mapper.readValue(jsonStr, new TypeReference<List<MyPojo>>() {});
MyPojo myPojo = myObjs.get(0);
myPojo.getData();
myPojo.getWhat();
// and so on
There are several libraries out there that will parse JSON, such as GSon and Jackson. If the functionality isn't provided out-of-box, you can write a custom deserializer that will do what you need.