2012-09-26 86 views
1

我想知道是否有辦法從JSONObject中獲取Vector。比方說,我有這個:從JSONObject獲取矢量對象

public class Foo 
    private JSONObject json; 
    public Foo(){ 
     try{ 
      json=new JSONObject(); 
      Vector<,F> v=new Vector<,F>(); // pretend like the comma isn't there please 
      json.put("blah", v); ...} catch (JSONException e){...} 
    } 
    . 
    . 
    . 
    public void addBlahs(,F goo){ 
     try{ 
      Object o=json.get("blah"); 
      // Since json.get("blah") should be a Vector of .F's, I thought I could do something like this... 
      Vector<,F> v=(Vector <,F>) o; 
      v.add(goo);} catch (JSONException{ ...} 
    } 

Eclipse給我一個警告說未經檢查的類型轉換。是否有可能從JSONObject中獲得某種類型的對象,然後才能夠使用該對象?我想向該Vector添加「goo」,但不知道如何正確訪問它並添加到該Vector。

我是一個初學者這樣下去容易對我:)

回答

0

你不能把一個VectorJSONObject。據JSONObject#put(String, Object)文檔:

關鍵 - 一個關鍵的字符串。

value - 一個值的對象。 它應該是下列其中一種類型:布爾型,雙精度型,整型, JSONArray,JSONObject,Long,String或JSONObject.NULL對象。

但是,您可以使用JSONArray代替。