因为不能依赖indecies,所以必须使用另一个标识符。 最好的一个应该是“名称”字段。
要找到一个具有特定名称的对象,您必须迭代数组并检查每个对象,这是我的代码,它可能不是最好或最有效的方式,但它的工作原理。本例使用GSON,但它应该是很容易适应:
/**
* Function for getting an object with a specific name from an array.
*
* @param arr The JsonArray to check in.
* @param name The name to check for.
* @return The JsonObject with a matching name field or null if none where found.
*/
public static JsonObject getObjectWithName(JsonArray arr, String name)
{
//Iterate over all elements in that array
for(JsonElement elm : arr)
{
if(elm.isJsonObject()) //If the current element is an object.
{
JsonObject obj = elm.getAsJsonObject();
if(obj.has("name")) //If the object has a field named "name"
{
JsonElem