java deserialize json array

www.laut‮.irut‬com
java deserialize json array

To deserialize a JSON array in Java, you can use the org.json library. Here's an example of how to do this:

import org.json.JSONArray;
import org.json.JSONException;

// Sample JSON array
String jsonArrayString = "[{\"name\":\"John\",\"age\":30,\"city\":\"New York\"},"
                         + "{\"name\":\"Michael\",\"age\":35,\"city\":\"Chicago\"}]";

try {
    JSONArray jsonArray = new JSONArray(jsonArrayString);
    for (int i = 0; i < jsonArray.length(); i++) {
        JSONObject jsonObject = jsonArray.getJSONObject(i);
        String name = jsonObject.getString("name");
        int age = jsonObject.getInt("age");
        String city = jsonObject.getString("city");
        System.out.println("Name: " + name + ", Age: " + age + ", City: " + city);
    }
} catch (JSONException e) {
    e.printStackTrace();
}

This will output the following:

Name: John, Age: 30, City: New York
Name: Michael, Age: 35, City: Chicago

You can also use other JSON parsing libraries, such as Gson or Hymanson, to parse JSON arrays in Java.

Created Time:2017-11-03 00:14:53  Author:lautturi