2017-12-26 3 views
0

contactList = new ArrayList<>(); 여기에 정보를이 형식으로 저장합니다 : "name", value_for_name.ArrayList, 값만 인쇄하십시오

나는이 함수 내에서 내 contactList를 채울 :

protected Void doInBackground(Void... arg0) { 
      HttpHandler sh = new HttpHandler(); 

      // Making a request to url and getting response 
      String jsonStr = sh.makeServiceCall(url); 



      Log.e(TAG, "Response from url: " + jsonStr); 

      if (jsonStr != null) { 
       try { 
        JSONObject jsonObj = new JSONObject(jsonStr); 

        // Getting JSON Array node 
        JSONArray contacts = jsonObj.getJSONArray("results"); 

        // looping through All Results 
        for (int i = 0; i < contacts.length(); i++) { 
         JSONObject c = contacts.getJSONObject(i); 

         String icon = c.getString("icon"); 
         String id = c.getString("id"); 
         String name = c.getString("name"); 



         // tmp hash map for single contact 
         HashMap<String, String> contact = new HashMap<>(); 

         // adding each child node to HashMap key => value 
         // contact.put("id", id); 
         contact.put("name", name); 
         // contact.put("email", icon); 


         // adding contact to contact list 
         contactList.add(contact); 

        } 
       } catch (final JSONException e) { 
        Log.e(TAG, "Json parsing error: " + e.getMessage()); 
        runOnUiThread(new Runnable() { 
         @Override 
         public void run() { 
          Toast.makeText(getApplicationContext(), 
            "Json parsing error: " + e.getMessage(), 
            Toast.LENGTH_LONG) 
            .show(); 
         } 
        }); 

       } 
      } else { 
       Log.e(TAG, "Couldn't get json from server."); 
       runOnUiThread(new Runnable() { 
        @Override 
        public void run() { 
         Toast.makeText(getApplicationContext(), 
           "Couldn't get json from server. Check LogCat for possible errors!", 
           Toast.LENGTH_LONG) 
           .show(); 
        } 
       }); 

      } 

      return null; 
     } 

문제는 그 내가 값을 인쇄하려고하면

System.out.println(contactList.get(position)); 

출력이 형식에 : {이름 = " 푸 "} 난 단지 Foo 내가 함께도 시도 인쇄 할

: System.out.println(String.valueOf(contactList.get(position)));,536,그러나 나는 항상 전체 문자열을 얻습니다. {name="Foo"}

제발 도와 줄 수 있습니까? 문자열을 구문 분석해야합니까?

+2

'System.out.println (contactList.get (position) .getName())'. –

+1

"문자열을 파싱해야하나요?" 아니요, 최후의 수단으로 생각해야합니다. 그것은 부서지기 쉽다. –

+2

'contactList.get (position) .get ("name")' –

답변

2

시도 : 나는 당신이 배열에서 "X"위치에서 개체를 얻을 할 수 있도록 배열 목록으로 해시 맵을 가지고 있고 그 후 속성 이름으로 해시 맵에서 값을 가져 볼

System.out.println(contactList.get(position).get("name")); 

.