2017-11-18 10 views
0

나는 안드로이드 앱과 웹 서비스를 가지고있다. 일부 레지스터를 편집하려면 데이터베이스에서 전체 레코드를 가져 와서 모든 필드를 텍스트 편집에 넣어야합니다.android studio에서 JSON 객체의 필드를 가져 오는 방법은 무엇입니까?

웹 서비스는 안드로이드 응용 프로그램

[{"id":"6","0":"6","tipo":"No Oxidado","1":"No Oxidado","fecha":"2015-02-02","2":"2015-02-02","ubicacion":"-1.555505, -6.6171","3":"-1.555505, -6.6171","persona":"Laura Morales","4":"Laura Morales","fotografia":"-","5":"-"}] 

날이 돌아 그리고 나는 변수는이 JSON 문자열을 가지고 "결과"라고했다

나는 독립적 인 텍스트를 편집의 모든 필드를 넣어 어떻게 내 안드로이드 애플 리케이션에서? 같은

뭔가 : "결과가"내 JSON 문자열입니다

txtid.setText(result[0]); 
txtType.setText(result[1]); 
txtDate.setText(result[2]); 
txtLocation.setText(result[3]); 
txtPerson.setText(result[4]); 

.

답변

0

아래처럼 입력 객체 모델 생성 처음 GSON

전화 안드로이드 모델에 JSON 개체를 변환하는 라이브러리가있다 : 당신이 바인딩 Gson를 사용하여 다음

class InputModel { 
    String id; 
    String 0; //it's better to change this object name 
    String tipo; 
... 
} 

은 모델에 입력하십시오.

InputModel mInput = new Gson().fromJson(data); 

PS1 : 데이터가

PS2 사용자의 입력 문자열이다 : 당신이 당신의 입력의 이름이 입력 이름이 다를 수 원하는 경우, 당신은이 같은 주석을 사용할 수 있습니다 그 결과는 아래와 같습니다 :

@SerializedName("id") 
String productId; 
0

아래 코드를 시도하십시오. 당신이 배열에 하나 이상의 JSON 객체가있는 경우

String response = "[{"id":"6","0":"6","tipo":"No Oxidado","1":"No Oxidado","fecha":"2015-02-02","2":"2015-02-02","ubicacion":"-1.555505, -6.6171","3":"-1.555505, -6.6171","persona":"Laura Morales","4":"Laura Morales","fotografia":"-","5":"-"}]" 

try { 
     JSONArray jsonArray = new JSONArray(response); 
     for (int i =0; i<jsonArray.length();i++){ 
      JSONObject jsonObject = jsonArray.getJSONObject(i); 
      txtid.setText(jsonObject.getString("0")); 
      txtType.setText(jsonObject.getString("1")); 
      txtDate.setText(jsonObject.getString("2")); 
     } 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 

그리고 당신은 배열에 하나의 개체 만이있는 경우 다음 필요가 루프 사용 직접이 방법을 사용할 수 없습니다

try { 
     JSONArray jsonArray = new JSONArray(response); 

      JSONObject jsonObject = jsonArray.getJSONObject(0); 
      txtid.setText(jsonObject.getString("0")); 
      txtType.setText(jsonObject.getString("1")); 
      txtDate.setText(jsonObject.getString("2")); 

    } catch (JSONException e) { 
     e.printStackTrace(); 
    }