我有一个json文件,其结构如下:
{
"Otions": true
"Plastic": ""
"Houses": {
7: {
"name": "Akash Bhawan"
"MemBers": 3
"children": 1
}-
8: {
"name": "Ashiyana"
"memBers": 4
"children": 2
}-
9: {
"name": "Faruks Nest"
"memBers": 5
"children": 1
}-
Houses内的对象是可变的,可以相应地增加或减少,也可以更改名称。 不管怎么说,他们的名字","成员","孩子"是唯一的领域,并将永远在那里
我正在使用gson解析
@SerializedName("Otions")
private String Options;
@SerializedName("Plastic")
private String plastics;
@SerializedName("Houses")
private Houses houses;
我想知道我是否可以在散列表或其他方式中存储不同名称的对象?
答案 0 :(得分:1)
如果您无法更改结构,则必须以Hashmap
int
为object
。HashMap<int, Object> House;
。例如: -
$("body").niceScroll({horizrailenabled:false});
该对象将包含name,memBers,details等元素。
答案 1 :(得分:0)
房屋应该是这样的地图。
私人HashMap的房子。由于3:表示索引结构。 Gson将根据需要创建嵌套对象。
答案 2 :(得分:0)
请将“house”作为数组而不是对象。
{
"Options": true,
"Plastics": "",
"house": [{
"name": "Akash Bhawan",
"MemBers": 3,
"children": 1
}, {
"name": "Ashiyana",
"memBers": 4,
"children": 2
}, {
"name": "Faruks Nest",
"memBers": 5,
"children": 1
}]
}
答案 3 :(得分:0)
你应该做一些事情(如我的评论中所述):
JSONObject jsonHouses = jsonObject.getJSONObject("Houses");
Iterator<String> keys = jsonHouses.keys(); //this will be array of "7", "8", "9"
if (keys.hasNext()) {
JSONObject singleHouseObject = jsonHouses.getJSONObject(keys.next());
//now do whatever you want to do with singleHouseObject.
//this is the object which has "name", "MemBers", "children"
}