Ich benutze Spring Social FqlQuery, um Daten von Facebook zu erhalten. Hier ist die JSON-Antwort, die ich von Facebook bekomme. Mein Controller, wo ich Json Ausgabe hier bin immer ist,Konvertieren von JSON in Java-Objekte mit Googles Gson
fql = "SELECT work FROM user WHERE uid = me()";
facebook.fqlOperations().query(fql, new FqlResultMapper<Object>() {
public Object mapObject(FqlResult result) {
List list = (List) result.getObject("work");
for (Object object : list) {
JsonHelper jsonHelper = new JsonHelper();
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String jsonOutput = gson.toJson(object);
System.out.println(jsonOutput);
gson.fromJson(jsonOutput, JsonHelper.class);
}
System.out.printin innen für Schleifenausgänge mehrere json wie unten .:
{
"employer": {
"id": 129843057436,
"name": "www.metroplots.com"
},
"location": {
"id": 102186159822587,
"name": "Chennai, Tamil Nadu"
},
"position": {
"id": 108480125843293,
"name": "Web Developer"
},
"start_date": "2012-10-01",
"end_date": "2013-05-31"
}
{
"employer": {
"id": 520808381292985,
"name": "Federation of Indian Blood Donor Organizations"
},
"start_date": "0000-00",
"end_date": "0000-00"
}
Hier ist mein Helfer Klasse:
import java.util.List;
public class JsonHelper {
class Employer{
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
class Location{
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
class Position{
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
//Edited After here
private String start_Date;
private String end_Date;
private Employer employer;
private Location location;
private Position position;
public String getStart_Date() {
return start_Date;
}
public void setStart_Date(String start_Date) {
this.start_Date = start_Date;
}
public String getEnd_Date() {
return end_Date;
}
public void setEnd_Date(String end_Date) {
this.end_Date = end_Date;
}
public Employer getEmployer() {
return employer;
}
public void setEmployer(Employer employer) {
this.employer = employer;
}
public Location getLocation() {
return location;
}
public void setLocation(Location location) {
this.location = location;
}
public Position getPosition() {
return position;
}
public void setPosition(Position position) {
this.position = position;
}
}
Wenn ich versuche, die JSON-Objekte zu Java-Objekt wie oben getan zu konvertieren, bekomme ich diese Ausnahme.
Kann mir jemand helfen, wo ich falsch liege. Bitte helfen Sie mir, JSON zu Java-Objekten zu konvertieren. Hoffe meine Frage ist klar. Danke im Voraus.
EDIT TO CONTROLLER GEMACHT:
facebook.fqlOperations().query(fql, new FqlResultMapper<Object>() {
public Object mapObject(FqlResult result) {
List<JsonHelper> json = new ArrayList<JsonHelper>();
List list = (List) result.getObject("work");
for (Object object : list) {
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String jsonOutput = gson.toJson(object);
System.out.println(jsonOutput);
JsonHelper jsonHelper = gson.fromJson(jsonOutput, JsonHelper.class);
json.add(jsonHelper);
System.out.println(jsonHelper.getStart_Date());
}
for (JsonHelper jsonHelper : json) {
System.out.println(jsonHelper.getStart_Date());
}
return list;
}
});
Die json Sie im Beispiel oben erwähnt haben, ist eigentlich ein jsonOject und nicht jsonArray, und Sie versuchen, es in die Liste zu werfen, das ist nur möglich, jsonArray, nicht JSONObject – Jhanvi
Seit In der for-Schleife erhalten Sie jsonObject, also entfernen Sie in Ihrer JsonHelper-Klasse List oder erstellen Sie das json-Array in Ihrer for-Schleife und parsen Sie das gesamte Array zusammen. – Jhanvi
@Jhanvi: Ich habe meine POJO-Klasse bearbeitet, wie du gesagt hast. Ich bekomme jetzt keine Ausnahmen. Es bindet also die Werte, die ich denke. Nach dem Hinzufügen setze ich das JsonHelper-Objekt in ArrayList und wenn ich versuche, auf die Werte von Innenobjekten zuzugreifen, bekomme ich "null" als Ausgabe. Kannst du herausfinden, wo es falsch ist? –