2013-10-28 20 views
7

Sto utilizzando Spring Social FqlQuery per ottenere dati da Facebook. Ecco la risposta JSON che sto ricevendo da facebook. Il mio controller dove sto ottenendo uscita JSON è qui,Conversione di oggetti Json in oggetti Java utilizzando Google 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.println all'interno per ciclo restituisce JSON multipla, come di seguito .:

{ 
    "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" 
} 

Ecco la mia classe helper:

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; 
} 
} 

Quando provo a convertire gli oggetti json in oggetto java come sopra ho ricevuto questa eccezione.

HTTP Status 500 - Request processing failed; nested exception is com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 6 column 16 

Qualcuno può aiutarmi dove mi sbaglio. Per favore aiutami a convertire json in oggetti java. Spero che la mia domanda sia chiara. Grazie in anticipo.

modifica apportata al regolatore:

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; 
} 

}); 
+1

JSON che hai citato nell'esempio di cui sopra è in realtà un jsonOject e non jsonArray, e si sta cercando di gettare nella lista, questo è possibile solo per jsonArray, non JsonObject – Jhanvi

+0

Dal in ciclo for si ottiene jsonObject, quindi nella classe JsonHelper, rimuovere List o creare json array nel ciclo for e analizzare l'intero array. – Jhanvi

+0

@Jhanvi: ho modificato la mia classe POJO come hai detto tu. Non ricevo eccezioni ora. Quindi legando i valori penso. dopo aver aggiunto sto mettendo l'oggetto JsonHelper all'interno di arrayList e poi quando provo ad accedere ai valori dagli oggetti interni sto ottenendo "null" come output. Quindi puoi capire dove è sbagliato? –

risposta

1

poiché non sono avendo accesso effettivo api, così sto provando con valore statico nell'esempio. In primo luogo nella classe JsonHelper, sostituire tutto per long, poiché i valori citati in json sono di tipo long e String. Poi provare Come accennato qui sotto:

  String str = "{\n" 
      + " \"employer\": {\n" 
      + " \"id\": 129843057436,\n" 
      + " \"name\": \"www.metroplots.com\"\n" 
      + " },\n" 
      + " \"location\": {\n" 
      + " \"id\": 102186159822587,\n" 
      + " \"name\": \"Chennai, Tamil Nadu\"\n" 
      + " },\n" 
      + " \"position\": {\n" 
      + " \"id\": 108480125843293,\n" 
      + " \"name\": \"Web Developer\"\n" 
      + " },\n" 
      + " \"start_date\": \"2012-10-01\",\n" 
      + " \"end_date\": \"2013-05-31\"\n" 
      + "}"; 


    List<JsonHelper> json = new ArrayList<JsonHelper>(); 
    Gson gson = new Gson(); 
    JsonHelper users = gson.fromJson(str, JsonHelper.class); 
    json.add(users); 


    for (JsonHelper js_obj : json) { 
     System.out.println(js_obj.getEmployer().getId()); 
     System.out.println(js_obj.getEmployer().getName()); 
    } 
+0

Grazie per il vostro aiuto. Ho una risposta La variabile Json e la mia variabile pojo dovrebbero avere lo stesso nome. Se ha lo stesso nome, solo i suoi valori di acquisizione. Altrimenti null sta stampando ... –

+1

@VigneshGopalakrishnan Sì, in Gson, i nomi delle variabili devono essere uguali e anche il dataType deve essere corretto altrimenti crea problemi. – Jhanvi

Problemi correlati