2012-06-02 4 views
0

제목에서 언급했듯이. 서버에서 가져 오는 json 데이터를 어떻게 루프 할 수 있습니까? 내 난 후,이 질문에 대해 주어진 링크에 accroding 변경android에서 json 데이터를 반복하는 방법

public class Home extends Activity { 
Button btnLogout; 
ScrollView svHome; 
UserFunctions userFunctions; 
LoginActivity userid; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    userid = new LoginActivity(); 
    svHome = (ScrollView)findViewById(R.id.svHome); 
    setContentView(R.layout.home); 

    userFunctions = new UserFunctions(); 

    /***********************************************************/ 
      //here is where my above mentioned json data is 
    JSONObject json = userFunctions.homeData(); 

    try { 
     if(json != null && json.getString("success") != null) { 
      //login_error.setText(""); 
      String res = json.getString("success"); 
      //userid = json.getString("uid").toString(); 
      if(Integer.parseInt(res) == 1) { 
            //currently this only shows the first json object 
       Log.e("pla", json.toString()); 
      } else { 
       //login_error.setText(json.getString("error_msg")); 
      } 
     } else { 
      Toast.makeText(getBaseContext(), "No data", Toast.LENGTH_LONG).show(); 
     } 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
    /*******************************************************/ 
} 

} 

업데이트 내 JSON 데이터

을 얻고 곳

여기
{ 
    "tag":"home", 
    "success":1, 
    "error":0, 
    "uid":"4fc8f94f1a51c5.32653037", 
    "name":"Saleem", 
    "profile_photo":"http:\/\/example.info\/android\/profile_photos\/profile1.jpg", 
    "places": 
    { 
     "place_photo":"http:\/\/example.info\/android\/places_photos\/place1.jpg", 
     "created_at":"2012-06-02 00:00:00", 
     "seeked":"0" 
    } 
} 
{ 
    "tag":"home", 
    "success":1, 
    "error":0, 
    "uid":"4fc8f94f1a51c5.32653037", 
    "name":"Name", 
    "profile_photo":"http:\/\/example.info\/android\/profile_photos\/profile1.jpg", 
    "places": 
    { 
     "place_photo":"http:\/\/example.info\/android\/places_photos\/place1.jpg", 
     "created_at":"2012-06-02 00:00:00", 
     "seeked":"0" 
    } 
} 
{ 
    "tag":"home", 
    "success":1, 
    "error":0, 
    "uid":"4fc8f94f1a51c5.32653037", 
    "name":"Name", 
    "profile_photo":"http:\/\/example.info\/android\/profile_photos\/profile1.jpg", 
    "places": 
    { 
     "place_photo":"http:\/\/example.info\/android\/places_photos\/place1.jpg", 
     "created_at":"2012-06-02 00:00:00", 
     "seeked":"0" 
    } 
} 

는 JSON 데이터의 종류를 받고. 여기 내 변경 사항은

/***********************************************************/ 
    JSONObject json = userFunctions.homeData(); 
    String jsonData = json.toString(); 

    try { 
     if(json != null && json.getString("success") != null) { 
      //login_error.setText(""); 
      String res = json.getString("success"); 
      //userid = json.getString("uid").toString(); 
      if(Integer.parseInt(res) == 1) { 
       JSONArray jsonArray = new JSONArray(jsonData); 
       for (int i = 0; i < jsonArray.length(); i++) { 
        JSONObject jsonObject = jsonArray.getJSONObject(i); 
        Log.e("Object", jsonObject.getString("places")); 
        //Log.i(ParseJSON.class.getName(), jsonObject.getString("text")); 
       } 
       Log.e("pla", json.toString()); 
      } else { 
       //login_error.setText(json.getString("error_msg")); 
      } 
     } else { 
      Toast.makeText(getBaseContext(), "No data", Toast.LENGTH_LONG).show(); 
     } 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
+0

json에서 변경할 수 있습니까? json에는 배열 중괄호 "[" "]"가 없으므로? –

답변

0

당신은 다음과 같은 JSON 라이브러리를 사용할 수 있습니다

수입 org.json.JSONArray을; import org.json.JSONObject; 이 같은 배열로 JSON 데이터를 읽을 수 있도록

:

JSONArray jsonArray 새로운 JSONArray = ([하여 JSON 데이터]);

이 튜토리얼보십시오 : http://www.vogella.com/articles/AndroidJSON/article.html

+0

위의 업데이트를 참조하십시오. 위의 링크에 따라 변경 한 후 logcat을 얻었습니다 – 2619

+0

json을 변경할 수 있습니까? json에는 배열 중괄호 "[" "]"가 없으므로? –

+0

어떻게 할 수 있습니까? – 2619

1

이 링크를 통해 찾아주세요

http://www.androidhive.info/2012/01/android-json-parsing-tutorial/

에 "[" "]"JSON에 어떤 배열 중괄호가 없기 때문에, JSON의 변경을 가능하면 당신은

JSON 그

{ 
"arrayKey": [ 
    { 
     "tag": "home", 
     "success": 1, 
     "error": 0, 
     "uid": "4fc8f94f1a51c5.32653037", 
     "name": "Saleem", 
     "profile_photo": "http://example.info/android/profile_photos/profile1.jpg", 
     "places": { 
      "place_photo": "http://example.info/android/places_photos/place1.jpg", 
      "created_at": "2012-06-02 00:00:00", 
      "seeked": "0" 
     } 
    }, 
    { 
     "tag": "home", 
     "success": 1, 
     "error": 0, 
     "uid": "4fc8f94f1a51c5.32653037", 
     "name": "Saleem", 
     "profile_photo": "http://example.info/android/profile_photos/profile1.jpg", 
     "places": { 
      "place_photo": "http://example.info/android/places_photos/place1.jpg", 
      "created_at": "2012-06-02 00:00:00", 
      "seeked": "0" 
     } 
    } 
] 
같아야 루프에서 itrate에 그 필요

} 당신의 로그 캣 상태로

+0

방금 ​​언급 한 형식으로 내 json을 변경할 수있는 링크를 제공 할 수 있습니까? 사전에 감사드립니다 – 2619

+0

json 서버에서 오는 것 같아요 ............ 그렇다면 그것은 서버 측 언어에 따라 달라집니다 ...... –

+0

나는 서버 쪽에서 PHP를 사용하고 있습니다 – 2619

0

, 당신은 JSONArray로 된 JSONObject로 변환하려는 :

org.json.JSONException: Value {"uid":"4fc8f94f1a51c5.32653037","places":{"place_photo":"http://example.info/android/places_photos/place1.jpg","created_at":"2012-06-02 00:00:00","seeked":"0","longitude":"24.943514","latitude":"60.167112"},"error":0,"success":1,"tag":"home","profile_photo":"http://example.info/android/profile_photos/profile1.jpg","name":"Zafar Saleem"} of type org.json.JSONObject cannot be converted to JSONArray 

시도를 코드를 디버깅 할 수 - 된 JSONObject를 찾아 예외가 발생하고, 확인 거기에 JSONArray 대신.

0

다른 방법으로는 추천합니다. GSON 라이브러리를 사용하면 쉽게 끝내기가 쉽습니다. 잘 포맷 된 Json의 경우

관련 문제