2014-10-04 1 views

risposta

15

Considerando che non si dispone di un POJO che descrive la struttura dei dati, si può semplicemente do:

final String json = "{\"contentType\": \"foo\", \"fooField1\": ... }"; 
final ObjectNode node = new ObjectMapper().readValue(json, ObjectNode.class); 

if (node.has("contentType")) { 
    System.out.println("contentType: " + node.get("contentType")); 
}  
2

Se si utilizza vasetti JSON nell'applicazione quindi il seguente frammento di codice è utile:

String json = "{\"contentType\": \"foo\", \"fooField1\": ... }"; 
JSONObject jsonObject = new JSONObject(json); 
System.out.println(jsonObject.getString("contentType")); 

e se si utilizza vasetti GSON poi lo stesso codice sarà simile seguente:

Gson gson = new GsonBuilder().create(); 
Map jsonMap = gson.fromJson(json, Map.class); 
System.out.println(jsonMap.get("contentType"));