我需要將這個king json文件加載到Java中的某個對象中。從json中加載元素列表
[
{
"id": 1,
"name": "FA1_M",
"ring_number": 1,
"type": 3,
"unit_index": 1,
"device_type": 3,
"address": 1,
"index_ring": 1,
"order": 0
},
{
"id": 2,
"name": "A1_1_LED",
"ring_number": 1,
"type": 4,
"unit_index": 1,
"device_type": 3,
"address": 1,
"index_ring": 2,
"order": 1
},
{
"id": 3,
"name": "FA1_A",
"ring_number": 1,
"type": 3,
"unit_index": 1,
"device_type": 3,
"address": 2,
"index_ring": 3,
"order": 2
},
{
"id": 4,
"name": "A1_2_LED",
"ring_number": 1,
"type": 4,
"unit_index": 1,
"device_type": 3,
"address": 2,
"index_ring": 4,
"order": 3
},
{
"id": 5,
"name": "FA1_BMA",
"ring_number": 1,
"type": 3,
"unit_index": 1,
"device_type": 3,
"address": 3,
"index_ring": 5,
"order": 4
},
{
"id": 6,
"name": "A1_3_LED",
"ring_number": 1,
"type": 4,
"unit_index": 1,
"device_type": 3,
"address": 3,
"index_ring": 6,
"order": 5
},
{
"id": 7,
"name": "Reserve_IN_4",
"ring_number": 1,
"type": 3,
"unit_index": 1,
"device_type": 3,
"address": 4,
"index_ring": 7,
"order": 6
},
{
"id": 8,
"name": "A1_4_LED",
"ring_number": 1,
"type": 4,
"unit_index": 1,
"device_type": 3,
"address": 4,
"index_ring": 8,
"order": 7
},
{
"id": 9,
"name": "Reserve_IN_5",
"ring_number": 1,
"type": 3,
"unit_index": 1,
"device_type": 3,
"address": 5,
"index_ring": 9,
"order": 8
},
{
"id": 10,
"name": "A1_5_LED",
"ring_number": 1,
"type": 4,
"unit_index": 1,
"device_type": 3,
"address": 5,
"index_ring": 10,
"order": 9
},
{
"id": 11,
"name": "Reserve_IN_6",
"ring_number": 1,
"type": 3,
"unit_index": 1,
"device_type": 3,
"address": 6,
"index_ring": 11,
"order": 10
},
{
"id": 12,
"name": "Reserve_OUT_6",
"ring_number": 1,
"type": 4,
"unit_index": 1,
"device_type": 3,
"address": 6,
"index_ring": 12,
"order": 11
},
{
"id": 13,
"name": "Reserve_IN_7",
"ring_number": 1,
"type": 3,
"unit_index": 1,
"device_type": 3,
"address": 7,
"index_ring": 13,
"order": 12
},
{
"id": 14,
"name": "KK1_ENB",
"ring_number": 1,
"type": 4,
"unit_index": 1,
"device_type": 3,
"address": 7,
"index_ring": 14,
"order": 13
},
{
"id": 15,
"name": "FA2_BMA",
"ring_number": 1,
"type": 3,
"unit_index": 1,
"device_type": 3,
"address": 8,
"index_ring": 15,
"order": 14
},
{
"id": 16,
"name": "KK2_ENB",
"ring_number": 1,
"type": 4,
"unit_index": 1,
"device_type": 3,
"address": 8,
"index_ring": 16,
"order": 15
},
{
"id": 17,
"name": "Smoke1_1",
"ring_number": 1,
"type": 2,
"unit_index": 2,
"device_type": 2,
"address": 1,
"index_ring": 17,
"order": 16
},
{
"id": 18,
"name": "Smoke1_2",
"ring_number": 1,
"type": 2,
"unit_index": 3,
"device_type": 12,
"address": 1,
"index_ring": 18,
"order": 17
},
{
"id": 19,
"name": "Smoke1_3",
"ring_number": 1,
"type": 2,
"unit_index": 4,
"device_type": 2,
"address": 1,
"index_ring": 19,
"order": 18
},
{
"id": 20,
"name": "Smoke1_4",
"ring_number": 1,
"type": 2,
"unit_index": 5,
"device_type": 2,
"address": 1,
"index_ring": 20,
"order": 19
},
{
"id": 21,
"name": "Fire1_1",
"ring_number": 1,
"type": 1,
"unit_index": 6,
"device_type": 4,
"address": 1,
"index_ring": 21,
"order": 20
},
{
"id": 22,
"name": "Fire1_2",
"ring_number": 1,
"type": 1,
"unit_index": 6,
"device_type": 4,
"address": 2,
"index_ring": 22,
"order": 21
},
{
"id": 23,
"name": "Fire1_3",
"ring_number": 1,
"type": 1,
"unit_index": 7,
"device_type": 1,
"address": 1,
"index_ring": 23,
"order": 22
},
{
"id": 24,
"name": "Motorless1",
"ring_number": 1,
"type": 5,
"unit_index": 8,
"device_type": 13,
"address": 1,
"index_ring": 24,
"order": 23
},
{
"id": 25,
"name": "Fire1_4",
"ring_number": 1,
"type": 1,
"unit_index": 9,
"device_type": 14,
"address": 1,
"index_ring": 25,
"order": 24
},
{
"id": 26,
"name": "Fire1_5",
"ring_number": 1,
"type": 1,
"unit_index": 9,
"device_type": 14,
"address": 2,
"index_ring": 26,
"order": 25
}
]
我有類元素,描述一個元素
public class element {
protected int id;
protected int type;
protected String state;
protected int status;
protected int device_type;
private Map<String, Integer>mask;
public int getId(){return this.id;}
public void setId(int id){this.id=id;}
public int getType(){return this.type;}
public void setType(int type){this.type=type;}
public String getState(){return this.state;}
public void setState(String state){this.state=state;}
public int getStatus(){return this.status;}
public void setStatus(int status){this.status=status;}
public int getDeviceType(){return this.device_type;}
public void setDeviceType(int device_type){this.device_type=device_type;}
public Map<String,Integer> getMask(){return this.mask;}
public void setMask(Map<String,Integer> mask){this.mask=mask;}
//index start by 1, not 0
public boolean getBit(int index){
return ((this.status>>(index-1)) & 0x1)==1;
}
public ElementView generateView(){
return null;
}
}
如何在元素列表加載JSON?我使用GSON查看了stackoverflow questoion,但是需要在json中使用這個數組的名稱,而我沒有這個名稱。任何人都可以幫忙嗎?
聲明「需要h在json中給這個數組命名「並不正確。下面,adisembiring發佈了一個反序列化到列表的合理解決方案。還有一個問題是原始問題中的JSON結構與Java結構不匹配,並且不清楚每個JSON元素應該如何綁定到各種Java組件。 – 2011-06-15 17:24:11