Home Convert JSON String to List of Java Objects
Reply: 0

Convert JSON String to List of Java Objects

user1120 Published in July 15, 2018, 9:20 pm

I have a simple JSON string in the format of:

  "obj_1": {
    "k1": "v1",
    "k2": "v2",
    "k3": "v3"
  "obj_2": {
    "k1": "v1",
    "k2": "v2",
    "k3": "v3"

And a class to represent each JSON object like so:

public class SomeObject {

public int k1;
public String k2;
public String k3;

public Constructor(int k1, String k2, String k3) {
    this.k1 = k1;
    this.k2 = k2;
    this.k3 = k3;

I want to read the JSON object into an array of those objects without having to loop using while loops as this takes a really long time.

JSONObject jsonObject = new JSONObject(jsonString);
SomeObject[] objectsList = gson.fromJson(jsonObject.toString(), SomeObject[].class);

The main idea is to populate each JSON object into the Java Class then into an array of SomeObject. This way inside the array i will have access to each object and access the properties using methods.

I end up getting an error message:

can't make objects of type SomeObject[]
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.329392 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO