我有以下字符串格式的json。如何將其轉換爲java對象?如何將Json字符串轉換爲Java對象?
JSON Srting
String allCameraList = [{
"name": "Camera1",
"displayURL": "Stream-1"
}, {
"name": "Camera2",
"displayURL": "Stream-3"
}, {
"name": "Camera4",
"displayURL": "Stream-7"
}, {
"name": "Camera3",
"displayURL": "Stream-5"
}, {
"name": "Camera5",
"displayURL": "Stream-10"
}, {
"name": "Camera6",
"displayURL": "Stream-12"
}]
的Java實體類
public class CameraDetails
{
private String name;
private String displayURL;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDisplayURL() {
return displayURL;
}
public void setDisplayURL(String displayURL) {
this.displayURL = displayURL;
}}
當我試圖使用JSON解析器像以下:
JsonParser parser = new JsonParser();
String json = parser.parse(allCameraList)
.getAsJsonObject()
.getAsJsonObject("name")
.toString();
它扔以下excep重刑:
java.lang.IllegalStateException: Not a JSON Object
請注意,我的JSON字符串是開始[
不{
。