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 Overflowandroid - Convert JSON String to List of Java Objects - Stack Overflow
How to convert list data into json in java - Stack Overflow
Convert JSON data to a list of Java objects - Stack Overflow
Convert this JSON data to list of objects in Java - Stack Overflow
Videos
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>>(){});
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));
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
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" }
]
}
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.
Use Jackson or GSON to read the JSON as List or array of expected type.
With Jackson:
ObjectMapper mapper = new ObjectMapper(); // reuse if multiple calls
MyPOJO[] pojos = mapper.readValue(jsonSource, MyPojo[].class); // with `List`, need to use `TypeReference`
// or, if you do not have matching MyPOJO type:
List<Object> pojosAsMaps = mapper.readValue(jsonSource, List.class);
There are many other ways you could do it, like reading as JSON tree (JsonNode).
It all depends on whether you can easily model structure as Java class (if so, most convenient) or not.
Just use the org.json.* package.
You can look it up under http://www.json.org/javadoc/org/json/package-summary.html JSONArray and JSONObject will be the most interesting code snippets.
Also your JSON-Object is not really well formated, you should make an Array of your Objects first, like:
users : [{
"shortname" : "g",
"moreAttributes" : ""
}, {
"shortname" : "h",
"moreAttributes" : ""
}]
Using this array-structure you can add objects information outside the array. For example an errormessage.
now you can access your json-file with some simple methods:
JSONObject jObj = //the json file you loaded
JSONArray jArr = jObj.getArray("users");
JSONObject jUser;
//at this point you can iterate over your array and get information via:
jUser = jArr.getJSONObject(1)
//now you can get all the information from the jUser object
//e.g.
jUser.getString("shortname");