2016-04-30 7 views
0

私のアンドロイドアプリにjsonコンテンツをパースしようとしています。 Iamはfirebase DBを使用して結果を取得します。キーなしでJsonObjectを解析する

{ 
"-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ファイルですが、どのように私はちょうどつかむことができます:私は が、私はここに、このJSONレスポンスを持っているということです必要なもの

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); 

} 

持って何

値?だから私は値だけの配列を持つことができます。

答えて

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 Arrayはありませんので、1つ1つのオブジェクトキーを解析します。そのダイナミックなと私はそれを制御カント - getJSONObject(「KFOmfn3XcmM3BsrWYcZ」):この

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

ような何か問題は、私は知らないです。 – neotrix3

関連する問題