2013-07-10 108 views
0

我試圖從JSON API中獲取所有值,我設法獲得了幾乎所有的值,除了一個!正如你所看到的,這是服務器的JSON輸出。 (我不能改變它)無法從JSON獲取上一個值

{ 
    "error":"", 
    "S8tf":{ 
     "infoToken":"wCfhXe", 
     "deleteToken":"gzHTfGcF", 
     "size":122484, 
     "sha1":"8c4e2bbc0794d2bd4f901a36627e555c068a94e6", 
     "filename":"Screen_Shot_2013-07-02_at_3.52.23_PM.png" 
    }, 
    "S29N":{ 
     "infoToken":"joRm6p", 
     "deleteToken":"IL5STLhq", 
     "size":129332, 
     "sha1":"b4a03897121d0320b82059c36f7a10a8ef4c113d", 
     "filename":"Stockholmsyndromet.docx" 
    } 
} 

正如你可以看到,每一個串/「陣列」帶「FILEID」這是隨機地從服務器生成開始。我使用下面的代碼,我可以看到所有的值,如:文件名,大小,sh1等。但我似乎無法弄清楚如何獲得「fileId」。該FILEID是(在此JSON)S8tf和S29N

我的代碼:

public class FilesActivity extends SherlockActivity { 

    private static String TAG_FILENAME = "filename"; 
    private static String TAG_SIZE = "size"; 
    private static String TAG_ITOKEN = "infoToken"; 
    private static String TAG_DTOKEN = "deleteToken"; 
    private static String TAG_SHA1 = "sha1"; 

    private ListView lv; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.dblist); 

     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
     getSupportActionBar().setTitle("Files"); 

     String response = null; 
     DefaultHttpClient httpClient = new DefaultHttpClient(); 
     ResponseHandler <String> resonseHandler = new BasicResponseHandler(); 
     HttpPost postMethod = new HttpPost("http://api.bayfiles.net/v1/account/files?session=of1903u3pj43c3can8rc33gc42"); 

     try { 
      JSONObject json = new JSONObject(); 
       json.put("filename", "error"); 

       postMethod.setEntity(new ByteArrayEntity(json.toString().getBytes("UTF8"))); 
       postMethod.setHeader("Content-Type", "application/json"); 
       response = httpClient.execute(postMethod,resonseHandler); 
       TextView txt = (TextView)findViewById(R.id.nodata); 
       JSONObject request = new JSONObject(response); 
       for (Iterator<?> keyIterator = request.keys(); keyIterator.hasNext();) { 
        String key = (String) keyIterator.next(); 
        JSONObject object = request.optJSONObject(key); 

        ArrayList<fileObject> objectList = new ArrayList<fileObject>(); 
        if (object != null) { 
         //Setting TAGs 
         TAG_FILENAME = object.getString("filename"); 
         TAG_SIZE = object.getString("size"); 
         TAG_ITOKEN = object.getString("infoToken"); 
         TAG_DTOKEN = object.getString("deleteToken"); 
         TAG_SHA1 = object.getString("sha1");    


         txt.setText(
           TAG_FILENAME + "\n" 
           + TAG_SIZE + "\n" 
           + TAG_ITOKEN + "\n" 
           + TAG_DTOKEN + "\n" 
           + TAG_SHA1 + "\n" 
           + txt.getText() 
           );      

         Log.d("log_tag", object.getString("filename")); 
        } 
       }   
     } 
     catch(Exception e) 
     {  
      e.printStackTrace(); 
      Log.d("log_tag", "Error: " + e.toString()); 

     } 
    } 
} 

如果你沒有得到它,我想抓住從FILEID又名S8tf和S29N的價值,這些都是隨機數並由服務器生成。

編輯:得到它的工作感謝你們!因爲我知道海報搞不清楚,所以我做的是把String fileId = key;在我的對象循環,就像這樣:

if (object != null) { 
    fileObject obj = new fileObject(); 
    obj.setFileId(key); 
    obj.setFileName(object.getString("filename")); 
    obj.setSize(object.getString("size")); 
    obj.setInfoToken(object.getString("infoToken")); 
    obj.setDeleteToken(object.getString("deleteToken")); 
    obj.setSha1(object.getString("sha1")); 

    objectList.add(obj); 
    Log.d("fileId", key); // Shows both of the values! 
} 
+2

您的fileId位於'key'變量中。 – njzk2

回答

0

如果你看看你的for循環的第一部分...

JSONObject request = new JSONObject(response); 

for (Iterator<?> keyIterator = request.keys(); keyIterator.hasNext();) { 

     String key = (String) keyIterator.next(); 
     JSONObject object = request.optJSONObject(key); 

你要求所有的鍵從代表來自服務器的響應的JSONObject。這些鍵是您正在查找的值。如果你在你的循環的第一行後添加

System.out.println(key);

,你會看到打印出來的值(「錯誤」,「S8tf」,...)。

+0

System.out.orintln(key);只顯示錯誤,沒有更多 –

+0

對不起,我現在得到了兩個名稱,像S8tf和S29N –

+0

哦,對不起,忘了我們在Android ..調試並檢查鍵值或使用Log.v()看什麼價值是。是的,因爲你在一個for循環中,你應該得到多個日誌語句。這能解決你的問題嗎? – Joel