【问题标题】:Java get one JSONObject from a JsonArrayJava 从 JsonArray 中获取一个 JSONObject
【发布时间】:2016-05-24 01:34:53
【问题描述】:

我有一个 Json 数组,我只想从中获取一个 Json 对象。 在此示例中,我如何使用 Apple 获取对象

[
{
"name": "mango",
"use": "DA",
"date": "2011-09-26",
"seed": "31341"
},

{
"name": "apple",
"use": "DA",
"date": "2011-09-26",
"seed": "31341"
},

{
"name": "berry",
"use": "DA",
"date": "2011-09-26",
"seed": "31341"
}
]

以前我通过它的索引位置获取它,但由于 json 不能保证我的顺序/排列,这就是为什么我需要专门获取一个对象而不使用索引方法。

【问题讨论】:

标签: java json


【解决方案1】:

您可以使用循环遍历 JSONArray 中的每个项目,并找到哪个 JSONObject 具有您想要的键。

private int getPosition(JSONArray jsonArray) throws JSONException {
        for(int index = 0; index < jsonArray.length(); index++) {
            JSONObject jsonObject = jsonArray.getJSONObject(index);
            if(jsonObject.getString("name").equals("apple")) {
                return index; //this is the index of the JSONObject you want
            } 
        }
        return -1; //it wasn't found at all
    }

您也可以返回 JSONObject 而不是索引。只需更改方法签名中的返回类型即可:

private JSONObject getPosition(JSONArray jsonArray) throws JSONException {
        for(int index = 0; index < jsonArray.length(); index++) {
            JSONObject jsonObject = jsonArray.getJSONObject(index);
            if(jsonObject.getString("name").equals("apple")) {
                return jsonObject; //this is the index of the JSONObject you want
            } 
        }
        return null; //it wasn't found at all
    }

【讨论】:

  • 简单明了,无需编写任何额外的类,请更新您的答案并将返回类型更改为 JSONObject
  • 自己想办法真的不难,但是……我今晚感觉特别好;)
【解决方案2】:

因为您不能依赖于不雅,您将不得不使用另一个标识符。 最好的可能是“名称”字段。

要找到具有特定名称的一个对象,您必须遍历数组并检查每个对象,这是我的代码,它可能不是最好或最有效的方法,但它确实有效。该示例使用 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"
            {
                JsonElement objElm = obj.get("name"); //The value of that field

                //Check if the value is a string and if it equals the given name
                if(objElm.isJsonPrimitive() && objElm.getAsJsonPrimitive().isString() && objElm.getAsString().equals(name))
                {
                    return obj;
                }
            }
        }
    }

    //Nothing matching was found so return null
    return null;
}

【讨论】:

    【解决方案3】:

    您可以将jackson 库与ObjectMapper 一起使用

    // Create a pojo for the json object
    public class MyObject {
        public String name;
        public String use;
        public String date;
        public String seed;
    }
    
    ...
    public MyObject getApple(String jsonString) throws IOException {
        // the string type is MyObject array
        MyObject[] myObjects = new ObjectMapper().readValue(jsonString, MyObject[].class);
        for (MyObject myObject : myObjects ){
            if myObject.name.equals("apple") {
                return myObject;
            }
        }
        return null;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-29
      • 1970-01-01
      相关资源
      最近更新 更多