【问题标题】:Why I get the structure of the Json instead of just the value?为什么我得到 Json 的结构而不仅仅是值?
【发布时间】:2017-08-10 01:08:09
【问题描述】:

我认为已经了解如何获取不同的 Json 数据 Json 的括号。通过我的代码,我获得了我需要的数据,但我注意到了两件事。

1) 为什么我不能通过 getText() 指定我需要距离的文本值,因为当我编写代码时让我进入 getElements()。

2) 为什么如果我显示距离的值,它会为我提供所有从 Elements 开始的 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 + '\'' +
            '}';
}
}

时长类

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 + '\'' +
            '}';
}
}

【问题讨论】:

    标签: android json retrofit


    【解决方案1】:

    getElements(). 返回一个元素的数组列表,所以你不能只说

    getElements().getDuration().getText() 
    

    您必须在 json 中指定您尝试访问的元素的位置,它只有一个元素,因此正确的格式是

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

    或者如果超过一个,您可以查看按索引获取元素

    如果你在你的 json 中使用循环,它会返回一个 json 数组而不是一个 json 对象

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-10
      • 1970-01-01
      • 2017-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多