2017-08-09 90 views
0

我認爲已經知道如何獲得Json數據,其中不同的 這個Json的括號。用我的代碼,我獲得了我需要的數據,但是我注意到了兩件事。爲什麼我會得到Json的結構而不僅僅是價值?

1)爲什麼我不能指定我需要通過getText()獲得距離的文本值,因爲當我編寫代碼時,讓我在getElements()中。 2)爲什麼如果我顯示距離的值,它給了我所有的結構所有的JSON從元素開始,例如:[Elements {distance = Distance {text ='5.6km'}}]而不只是5.6公里

持續時間也一樣!

Retrofit retrofit = new Retrofit.Builder() 
              .baseUrl("https://maps.googleapis.com") 
              .addConverterFactory(GsonConverterFactory.create()) 
              .build(); 

            ApiInterface apiInterface = retrofit.create(ApiInterface.class); 

            Call<Feed> call = apiInterface.getData(); 

            call.enqueue(new Callback<Feed>() { 
             @Override 
             public void onResponse(Call<Feed> call, Response<Feed> response) { 

              Log.d(TAG, "onResponse: Server Response: "+response.toString()); 
              Log.d(TAG, "onResponse: received information: "+ response.body().toString()); 

              ArrayList<Rows> rowsList = response.body().getRows(); 
              ArrayList<String> destination_addresses_list = response.body().getDestination_addresses(); 

              for (int i=0; i<rowsList.size(); i++){ 



               Log.d(TAG, "onResponse: \n"+ 
               "destination_addresses: "+ destination_addresses_list.get(i)+"\n"+ 

               // this is where I cant specify getElements().getDistance().getText() 
               "distance"+ rowsList.get(i).getElements()+ 

               //And in this one too! getElements().getDuration().getText() 
               "duration"+ rowsList.get(i).getElements()); 

              } 
             } 

             @Override 
             public void onFailure(Call<Feed> call, Throwable t) { 

              Log.e(TAG, "onFailure: algo paso: "+ t.getMessage()); 
              Toast.makeText(getApplicationContext(), "error", Toast.LENGTH_LONG).show(); 
             } 
            }); 

飼料類:

public class Feed { 


@SerializedName("destination_addresses") 
@Expose 
private ArrayList<String> destination_addresses; 

// este contiene cas datos que se pueden separar 
@SerializedName("rows") 
@Expose 
private ArrayList<Rows> rows; 


public ArrayList<String> getDestination_addresses() { 
    return destination_addresses; 
} 

public void setDestination_addresses(ArrayList<String> destination_addresses) { 
    this.destination_addresses = destination_addresses; 
} 

public ArrayList<Rows> getRows() { 
    return rows; 
} 

public void setRows(ArrayList<Rows> rows) { 
    this.rows = rows; 
} 

@Override 
public String toString() { 
    return "Feed{" + 
      "destination_addresses=" + destination_addresses + 
      ", rows=" + rows + 
      '}'; 
} 
} 

行類

public class Rows { 

@SerializedName("elements") 
@Expose 
private ArrayList<Elements> elements; 

public ArrayList<Elements> getElements() { 
    return elements; 
} 

public void setElements(ArrayList<Elements> elements) { 
    this.elements = elements; 
} 

@Override 
public String toString() { 
    return "Rows{" + 
      "elements=" + elements + 
      '}'; 
} 
} 

元素類

public class Elements { 

@SerializedName("distance") 
@Expose 
private Distance distance; 


@SerializedName("duration") 
@Expose 
private Duration duration; 


public Distance getDistance() { 
    return distance; 
} 

public void setDistance(Distance distance) { 
    this.distance = distance; 
} 



public Duration getDuration() { 
    return duration; 
} 

public void setDuration(Duration duration) { 
    this.duration = duration; 
} 


@Override 
public String toString() { 
    return "Elements{" + 
      "distance=" + distance + 
      // ", duration=" + duration + 
      '}'; 
} 
} 

距離類

public class Distance { 

@SerializedName("text") 
@Expose 
private String text; 


public String getText() { 
    return text; 
} 

public void setText(String text) { 
    this.text = text; 
} 


@Override 
public String toString() { 
    return "Distance{" + 
      "text='" + text + '\'' + 
      '}'; 
} 
} 

Duration類

public class Duration { 


@SerializedName("text") 
@Expose 
private String text; 

public String getText() { 
    return text; 
} 

public void setText(String text) { 
    this.text = text; 
} 


@Override 
public String toString() { 
    return "Duration{" + 
      "text='" + text + '\'' + 
      '}'; 
} 
} 

回答

0

元素的getElements().返回一個數組列表,所以你不能只是說

getElements().getDuration().getText() 

你必須指定你要訪問的元素的位置你的json它只有一個元素,所以正確的格式是

getElements().get(0).getDuration().getText() 

或者你c一個使一看通過指數來獲得元素;如果超過一個

而且,如果你把循環在你的JSON它返回一個JSON數組不是一個JSON對象

+0

你吧!有用!謝謝。 – juan

相關問題