2016-02-12 71 views
2

我有一個Json數組,我只想從中獲取一個Json對象。 在這個例子中我如何可以獲取對象與蘋果Java從JsonArray中獲取一個JSONObject

[ 
{ 
"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不能保證我的訂單/安排,這就是爲什麼我需要特別得到一個對象,而使用索引方法。

+0

這應該是您的解決方案http://stackoverflow.com/questions/5288833/如何搜索json-tree-with-jquery – user3509208

+1

答案是關於jquery,這個問題是Java – tddmonkey

+0

是的,這是關於java沒有javascript – Tuna

回答

3

您可以使用循環遍歷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 
    } 
+0

簡單明瞭,沒有編寫任何額外的類,請更新你的答案,並改變返回鍵入到JSONObject – Tuna

+0

真的不難想你自己,但...今晚我感覺特別好;) – jaytj95

0

因爲不能依賴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" 
      { 
       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; 
} 
0

您可以使用jacksonObjectMapper

// 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; 
} 
相關問題