繁体   English   中英

如何使用翻新2正确使用Json Reader在Json中导航?

[英]How do I correctly use Json Reader to navigate in my Json using retrofit 2?

这是我的API

http://services.groupkt.com/country/get/all

我对如何使用Json阅读器方法感到困惑。 我尝试查看它的Javadoc,这看起来很简单,但是当我实现它时,它具有不同的行为。

这是我的代码

RestResponse result = null;
        String countryName = null;
        String alpha2Code = null;
        String alpha3Code = null;

    jsonReader.beginObject();
    jsonReader.beginArray();

    while (jsonReader.hasNext()) {
        countryName = jsonReader.nextString();
        alpha2Code = jsonReader.nextString();
        alpha3Code = jsonReader.nextString();
    }
    jsonReader.endArray();
    jsonReader.beginArray();

    while (jsonReader.hasNext()) {
        jsonReader.beginObject();
        while (jsonReader.hasNext()) {
            jsonReader.skipValue();
            jsonReader.peek();
        }
        jsonReader.endObject();
    }

    jsonReader.endArray();

这是我的代码,用于学习如何导航json。 此代码在TypeAdapter的read方法上运行。

您能提供我如何轻松理解如何正确使用json阅读器方法的示例吗?

轻松一点,只需尝试GSON。 有很多例子,关于这个的文章

https://guides.codepath.com/android/Consuming-APIs-with-Retrofit#overview https://medium.freecodecamp.com/rxandroid-and-retrofit-2-0-66dc52725fff#.ymmfqdi9s https:// zeroturnaround .COM / rebellabs /工具入门与-改装-2 /

根据http://services.groupkt.com/country/get/all的回应,这里是GSON模型

public class County {
    @SerializedName("name") public String name;
    @SerializedName("alpha2_code") public String alpha2Code;
    @SerializedName("alpha3_code") public String alpha3Code;
}

public class RestResponse {
    @SerializedName("messages") public Messages messages;
    @SerializedName("result") public Countries counties;
}

public class CountriesResponse {
    @SerializedName("RestResponse") public RestResponse restResponse;
}   

public interface GroupktApi {
  @GET("/country/get/all")
  Call<CountriesResponse> getAllCountries()
}

public Gson provideGson() {
return new GsonBuilder().registerTypeAdapter(Messages.class, MessagesDeserializer());
}

public class MessagesDeserializer extend JsonDeserializer<Messages> {

    @Override public Messages deserialize(JsonElement json, Type typeOfT,
      JsonDeserializationContext context) throws JsonParseException {
        List<String> messages = new ArrayList();
        if (json.isJsonArray()) {
          Type listType = new TypeToken<ArrayList<String>>(){}.getType();
          List<String> arrayMessages = context.deserialize<List<String>>(value, listType)
          messages.addAll(arrayMessages)
        } else {
          String message = json.asString()
          messages.add(message)
        }
        return new Messages(messages);
    }
}

public class Messages {
    public List<String> messages;

    public Messages (List<String> messages) {
       this.messages = messages;
    }
}

国家以同样的方式

而已

如果您想学习基本的jSON解析,则一定要阅读此Android Json解析 ...但是在翻新2中,您可以使用Model类而不是json解析.....我在下面共享我的代码。型号类别

public class WeatherResponse {

@SerializedName("cod")
@Expose
private String cod;
@SerializedName("message")
@Expose
private Double message;
@SerializedName("cnt")
@Expose
private Double cnt;
@SerializedName("list")
@Expose
private List<cityList> list = null;
@SerializedName("city")
@Expose
private City city;

public String getCod() {
    return cod;
}

public void setCod(String cod) {
    this.cod = cod;
}

public Double getMessage() {
    return message;
}

public void setMessage(Double message) {
    this.message = message;
}

public Double getCnt() {
    return cnt;
}

public void setCnt(Double cnt) {
    this.cnt = cnt;

API客户端

public class ApiClient {
private static final int TIME_OUT = 30;
public static final String BASE_URL = "http://api.openweathermap.org/";
private static Retrofit retrofit = null;


public static Retrofit getClient() {
    if (retrofit==null) {
        OkHttpClient.Builder okBuilder = new OkHttpClient.Builder();

        okBuilder.connectTimeout(TIME_OUT, TimeUnit.SECONDS);
        okBuilder.readTimeout(TIME_OUT, TimeUnit.SECONDS);
        okBuilder.writeTimeout(TIME_OUT, TimeUnit.SECONDS);

        Gson gson = new GsonBuilder().create();

        return new Retrofit.Builder()
                .baseUrl(BASE_URL) .addConverterFactory(ScalarsConverterFactory.create())
                .addConverterFactory(GsonConverterFactory.create(gson))
                .client(okBuilder.build())
                .build();
    }
    return retrofit;
}
}

API接口

public interface ApiInterface {
      @GET("data/2.5/forecast?id=524901")
      Call<WeatherResponse> getWeatherData(@Query("APPID") String apiKey);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM