ich einen Web-Service geschrieben haben, und in der folgenden URL http://192.168.2.34:8081/MyWebService/facility/universityParsing JSON Antwort mit Jackson
Antwort von diesem Web-Service im JSON-Format gehostet ist:
{
"university": [
{
"emailOfPrincipal": "[email protected]",
"labLists": [
{
"instrumentLists": [
{
"instrumentId": "1",
"instrumentName": "Instrument 1"
},
{
"instrumentId": "2",
"instrumentName": "instrument 2"
}
],
"labId": "11",
"labName": "Lab 11"
},
{
"instrumentLists": [
{
"instrumentId": "3",
"instrumentName": "instrument 3"
},
{
"instrumentId": "4",
"instrumentName": "instrument 4"
}
],
"labId": "22",
"labName": "Lab 22"
}
],
"univAddress": "Kolkata",
"univId": "111",
"univName": "University 111",
"univPrincipalName": "Principal 1"
},
{
"emailOfPrincipal": "[email protected]",
"labLists": [
{
"instrumentLists": [
{
"instrumentId": "5",
"instrumentName": "Instrument 5"
},
{
"instrumentId": "6",
"instrumentName": "Instrument 6"
}
],
"labId": "33",
"labName": "Lab 33"
},
{
"instrumentLists": [
{
"instrumentId": "7",
"instrumentName": "Instrument 7"
},
{
"instrumentId": "8",
"instrumentName": "Instrument 8"
}
],
"labId": "44",
"labName": "Lab 44"
}
],
"univAddress": "Bangalore",
"univId": "222",
"univName": "University 222",
"univPrincipalName": "Principal 2"
},
{
"emailOfPrincipal": "[email protected]",
"labLists": [
{
"instrumentLists": [
{
"instrumentId": "9",
"instrumentName": "Instrument 9"
},
{
"instrumentId": "10",
"instrumentName": "Instrument 10"
}
],
"labId": "55",
"labName": "Lab 55"
},
{
"instrumentLists": [
{
"instrumentId": "11",
"instrumentName": "Instrument 11"
},
{
"instrumentId": "12",
"instrumentName": "Instrument 12"
}
],
"labId": "66",
"labName": "Lab 66"
}
],
"univAddress": "Hyderabad",
"univId": "333",
"univName": "University 333",
"univPrincipalName": "Principal 3"
}
]
}
Ich versuche, JSON-Daten zu konvertieren in Java-Objekt mit Jackson-API. Aber einen Fehler bekommen. Meine Java-Objekte sind:
University.java
public class University {
private int univId;
private String univName;
private String univAddress;
private String univPrincipalName;
private String emailOfPrincipal;
private List<Laboratory> labLists = new ArrayList<>();
//... Getter and Setter Methods...
}
Laboratory.java
public class Laboratory {
private int labId;
private String labName;
private List<Instruments> instrumentLists = new ArrayList<>();
//... Getter and Setter Methods...
}
Instruments.java
public class Instruments {
private int instrumentId;
private String instrumentName;
private String bookingStatus;
private Date bookingFrom;
private Date bookingTo;
private Date requestedTime;
//... Getter and Setter Methods...
}
Der Kunde geschrieben ist:
public class ParseJSONData {
public static void main(String[] args) throws ClientProtocolException, IOException {
String URL = "http://192.168.2.34:8081/MyWebService/facility/university";
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(URL);
HttpResponse response = httpclient.execute(httpget);
// System.out.println(response);
HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity);
ObjectMapper mapper = new ObjectMapper();
University universities = mapper.readValue(result, University.class);
System.out.println(universities.getEmailOfPrincipal());
}
Der Fehler Ich erhalte sagt
Exception in thread "main" org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "university" (Class University), not marked as ignorable
at [Source: [email protected]; line: 1, column: 16] (through reference chain: University["university"])
Ich kenne meine JSON-Daten Liste der Hochschulen zurückkehrt. Aber wie man diese in der Liste abbildet, ist mir nicht bekannt. Jede Art von Hilfe wird sehr geschätzt.
Ah ... Das ist cool. Ich habe nicht darüber nachgedacht, ein zusätzliches Java-Objekt zu erstellen. Könntest du mich bitte auch umgekehrt wissen lassen, d. H., Indem ich einen benutzerdefinierten Deserializer für die Universität erstelle? –
@SwarupSaha Ich habe den Beitrag aktualisiert, aber Sie können mit Deserializern beliebig spielen. – varren