[英]JSON-Simple get array based on contents
所以,我有一個JSON文件,我想在主對象中加載一個數組,所以我用以下方法加載它:
try {
schemaData = (JSONObject) parser.parse(new FileReader(schema));
schemaItemDataArray = (JSONArray) schemaData.get("items");
} catch (IOException | ParseException e) {
e.printStackTrace();
}
我加載的名為“ items”的數組
schemaItemDataArray = (JSONArray) schemaData.get("items");
包含大量具有以下結構的無名對象:
{
"name": "TF_WEAPON_BAT",
"defindex": 0,
"item_class": "tf_weapon_bat",
"item_type_name": "Bat",
"item_name": "Bat",
"proper_name": false,
"item_slot": "melee",
"model_player": "models\/weapons\/w_models\/w_bat.mdl",
"item_quality": 0,
"image_inventory": "backpack\/weapons\/c_models\/c_bat",
"min_ilevel": 1,
"max_ilevel": 1,
"image_url": "http:\/\/media.steampowered.com\/apps\/440\/icons\/c_bat.d037d6a40ec30ab4aa009387d476dca889b6f7dc.png",
"image_url_large": "http:\/\/media.steampowered.com\/apps\/440\/icons\/c_bat_large.0ac4b6f335f671bd6b5e6ae02f47985af2da8c48.png",
"craft_class": "weapon",
"craft_material_type": "weapon",
"capabilities": {
"nameable": true,
"can_craft_mark": true,
"can_be_restored": true,
"strange_parts": true,
"can_card_upgrade": true,
"can_strangify": true,
"can_consume": true
},
"used_by_classes": [
"Scout"
]
}
我想要做的是獲取一個JSONObject,其中包含我通過過濾名稱值選擇的對象。 例如,我想獲取所有將“名稱”鍵設置為“ TF_WEAPON_BAT”的對象,然后將其存儲到名為FoundItem的JSONObject中。
在此先感謝Denton。
據我所知,該庫沒有XPath樣式解析器。 您將需要自己瀏覽條目。 例如
JSONObject object = new JSONObject("{\"items\": [{\"name\":\"TF_WEAPON_BAT\"}, {\"name\":\"TF_WEAPON_BAT\"}]}");
JSONArray array = object.getJSONArray("items");
for (int i = 0; i < array.length(); i++) {
String name = array.getJSONObject(i).getString("name");
if ("TF_WEAPON_BAT".equals(name)) {
// do something
JSONObject foundItem = array.getJSONObject(i); // this holds the object that had a matching name element
// you can add it to some other list
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.