这是我的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的读取方法上运行。
您能否提供一些示例,说明如何正确理解如何正确使用json reader方法?
答案 0 :(得分:0)
很简单,只需尝试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/getting-started-with-retrofit-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;
}
}
国家以同样的方式
那是
答案 1 :(得分:0)
如果你想学习基本的jSON解析,你一定要阅读这个Android Json Parsing .....但是在改造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);
}