2016-04-30 100 views
0

Iam試圖解析JSON內容到我的Android應用程序。 Iam使用firebase數據庫來獲得結果。JsonObject解析無鑰匙

我有什麼

private void StartParse() { 
     JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET, 
       webserviceUrl, null, new Response.Listener<JSONObject>() { 

     @Override 
     public void onResponse(JSONObject response) { 
      Log.d("Repsonse", response.toString()); 

      try { 
       // Parsing json object response 
       // response will be a json object 

       String cover = response.optString("cover").toString(); 
       String ip_address = response.optString("ip_address").toString(); 
       String port = response.optString("port").toString(); 
       String info = response.optString("info").toString(); 
       String uid = response.optString("uid").toString(); 

       Map<String, String> map = new HashMap<String, String>(); 
       map.put(cover, cover.toString()); 
       // and so on.. 
      } catch (Exception e) { 
       e.printStackTrace(); 
       Toast.makeText(getApplicationContext(), 
         "Error: " + e.getMessage(), 
         Toast.LENGTH_LONG).show(); 
      } 
     } 
    }, new Response.ErrorListener() { 

     @Override 
     public void onErrorResponse(VolleyError error) { 
      VolleyLog.d("Response", "Error: " + error.getMessage()); 
      Toast.makeText(getApplicationContext(), 
        error.getMessage(), Toast.LENGTH_SHORT).show(); 
      // hide the progress dialog 
     } 
    }); 

    // Adding request to request queue 
    AppController.getInstance().addToRequestQueue(jsonObjReq); 

} 

我需要什麼 的是,我這裏有此JSON響應:

{ 
"-KFOmfn3XcmM3BsrWYcZ":{ 
    "company":"A17", 
    "country":"France", 
    "email":"[email protected]", 
    "uid":"720363f7-84f1-4a35-9551-9422da991835" 
}, 
"-KTCLolTzHO0KFcuQR3B":{ 
    "company":"B13", 
    "country":"Sweden", 
    "email":"[email protected]", 
    "uid":"7eade882-c182-441e-8793-94c6a1879a52" 
}, 
"-KFcuQR3B1KTCLsa43BK10":{ 
    "company":"C23", 
    "country":"Denmark", 
    "email":"France", 
    "uid":"7eade882-c182-441e-8793-94c6a1879a52" 
}, 
"-CLHOcuQoKFzRTl3BKx0T":{ 
    "company":"4SS", 
    "country":"Italy", 
    "email":"[email protected]", 
    "uid":"7eade882-c182-441e-8793-94c6a1879a52" 
} 
} 

響應是JSON文件,但我如何才能抓住值?所以我可以只有一個數組。

回答

3
Iterator<String> iter = json.keys(); 
while (iter.hasNext()) { 
    String key = iter.next(); 
    try { 
     Object value = json.get(key); 
    } catch (JSONException e) { 
     // Something went wrong! 
    } 
} 
0

沒有Json數組,因此您必須逐個解析對象鍵。這樣

JSONObject jsonObject = new JSONObject(response); 
String company = jsonObject.getJSONObject("-KFOmfn3XcmM3BsrWYcZ").getString("company"); 
String country= jsonObject.getJSONObject("-KFOmfn3XcmM3BsrWYcZ").getString("country"); 
// email and uid same 
String company1 = jsonObject.getJSONObject("-KTCLolTzHO0KFcuQR3B").getString("company"); 
String country1= jsonObject.getJSONObject("-KTCLolTzHO0KFcuQR3B").getString("country"); 

...... 
+0

問題是東西我不知道:getJSONObject(「 - KFOmfn3XcmM3BsrWYcZ」),其動態和我無法控制它。 – neotrix3