2014-10-04 1408 views

回答

15

考慮到你沒有一個POJO描述你的數據結構,你可以簡單地做:

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

如果您正在使用JSON罐子在您的應用程序,然後將下面的代碼片段是有用的:

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

,如果你正在使用GSON罐則同樣的代碼將類似於以下:

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