2

I have a response like this:

[ { "key": { "kind": "UserRecord", "id": 0, "name": "1" }, "firstName": "1", "lastName": "1", "homeLat": 0.0, "homeLon": 0.0, "workLat": 0.0, "workLon": 0.0, "currentLat": 10.0, "currentLon": 10.82, "timestamp": 1335735046606, "score": 0, "isStarred": false, "distance": 0.0 }, { "key": { "kind": "UserRecord", "id": 0, "name": "32423542324234324" }, "firstName": "Simone", "lastName": "Boscolo Berto", "homeLat": 0.0, "homeLon": 0.0, "workLat": 0.0, "workLon": 0.0, "currentLat": 55.786444, "currentLon": 12.515867, "timestamp": 1335884083696, "score": 0, "isStarred": false, "distance": 0.0 } ]

and a method that from each JSONObject give me my object

private User getUserFromJson(JSONObject jsonUser)

How can I iterate in that list of JSONObjects? should I use JSONArrayobject?

3 Answers3

1

This could be easy with Gson or jackson

Gson example

Gson gson = new Gson();
User[] users = gson.fromJson(<json string>, User[].class);

Jackson example

ObjectMapper mapper = new ObjectMapper();  
Collection<User> users =  
    mapper.readValue(<json string>, `new TypeReference<Collection<User>>() {});  

Sample -

package com.test;

import com.google.gson.Gson;

public class GSonExample {
    public static void main(String[] args) {
        String json = "{\"name\":\"Duke\",\"address\":\"Menlo Park\",\"dateOfBirth\":\"Feb 1, 2000 12:00:00 AM\"}";

        Gson gson = new Gson();
        User student = gson.fromJson(json, User.class);

        System.out.println("student.getName()        = " + student.getName());
        System.out.println("student.getAddress()     = " + student.getAddress());
        System.out.println("student.getDateOfBirth() = " + student.getDateOfBirth());
    }
}

public class User {
    private String name;
    private String address;
    private String dateOfBirth;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public String getDateOfBirth() {
        return dateOfBirth;
    }
    public void setDateOfBirth(String dateOfBirth) {
        this.dateOfBirth = dateOfBirth;
    }
}

classpath

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry kind="src" path="src"/>
    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
    <classpathentry kind="lib" path="lib/gson-2.1.jar"/>
    <classpathentry kind="output" path="bin"/>
</classpath>
Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103
  • I'm trying the Gson method, because it seem very useful, but I encurred in that problem: "Could not find class 'com.google.gson.Gson', referenced from my package. Btw, I included the library and it compile, it give that problem only run time – Simone.bb2003 May 02 '12 at 06:38
  • it seems surprising because if you added in to build path it should not popup this error. you can check this [thread](http://stackoverflow.com/questions/4961336/i-am-getting-java-lang-classnotfoundexception-com-google-gson-gson-error-even) – Subhrajyoti Majumder May 02 '12 at 06:43
  • I already took a look in that thread, but is not helping because I have not a Servlet\JSP – Simone.bb2003 May 02 '12 at 06:53
  • @Simone.bb2003 - I have updated a sample. I hope this will be helpful for you. – Subhrajyoti Majumder May 02 '12 at 09:03
0

Something like this:

final String incomingJSON ;
final JSONArray objArray = new JSONArray(incomingJSON);
for(int i = 0; i < objArray.length(); i++) {
    final JSONObject obj = objArray.getJSONObject(i);
    final User user = getUserFromJson(obj);
}
Perception
  • 79,279
  • 19
  • 185
  • 195
0

Are you using the Gson api? With that, you can get an iterator from a JsonArray object.

Iterator<JsonElement>   iterator()
          Returns an iterator to navigate the elemetns of the array.
Carl S
  • 61
  • 5