Android 当Json内容以object({)开头时,我可以使用Kotlin访问
我可以像这样正常访问Json:Android 当Json内容以object({)开头时,我可以使用Kotlin访问,android,json,kotlin,Android,Json,Kotlin,我可以像这样正常访问Json: [ { "name": "Algeria", "capital": "Algiers", "region": "Africa", "currency": "DZD", "flag": "https://raw.githubusercontent.com/atilsamancioglu/IA19-DataSetCountries/master/dza.png", "language": "Arabic"
[
{
"name": "Algeria",
"capital": "Algiers",
"region": "Africa",
"currency": "DZD",
"flag": "https://raw.githubusercontent.com/atilsamancioglu/IA19-DataSetCountries/master/dza.png",
"language": "Arabic"
},
{
"name": "Andorra",
"capital": "Andorra Vella",
"region": "Europe",
"currency": "EUR",
"flag": "https://raw.githubusercontent.com/atilsamancioglu/IA19-DataSetCountries/master/and.png",
"language": "Catalan"
}
]
但如果Json像这样启动,我无法访问:
{
"responseCode": 200,
"errors": null,
"message": null,
"result": {
"requestId": "360017149b13663c997de5",
"offers": {
"hotels": [
{
"id": 232997,
"details": {
"id": null,
"slug": "grand-park-bodrum-232997",
"address": {
"address": "Peksimet Mah. Bozdağ 12. Sk. No: 7/1 48960 Bodrum-Muğla",
"zipCode": null,
"country": {
"code": "TR",
"name": "Türkiye"
},
"countryCode": "TR",
"town": {
"id": null,
"name": "Bodrum"
},
"city": {
"id": null,
"name": "Muğla"
}
}
我的代码如下:
型号.kt
data class Otel(
@SerializedName("errors")
val errors: Any,
@SerializedName("message")
val message: Any,
@SerializedName("responseCode")
val responseCode: Int,
@SerializedName("result")
val result: Result
) {
data class Result(
@SerializedName("filters")
val filters: Filters,
@SerializedName("offers")
val offers: Offers,
@SerializedName("requestId")
val requestId: String,
@SerializedName("sortingMethods")
val sortingMethods: List<SortingMethod>
) { continious like this
数据类奥特尔酒店(
@SerializedName(“错误”)
val错误:任何,
@SerializedName(“消息”)
val消息:任何,
@序列化名称(“响应代码”)
val响应代码:Int,
@SerializedName(“结果”)
val结果:结果
) {
数据类结果(
@SerializedName(“过滤器”)
val过滤器:过滤器,
@序列化名称(“提供”)
val报价:报价,
@SerializedName(“请求ID”)
val requestId:字符串,
@SerializedName(“排序方法”)
val排序方法:列表
){像这样继续
OtelAPI
interface OtelAPI {
@GET("otel.json")
fun getOtels(): Single<List<Otel>>
}
接口OtelAPI{
@获取(“otel.json”)
fun getOtels():单身
}
OtelAPIService
class OtelAPIService {
private val BASE_URL = "http://www.ipucubilisim.com.tr/"
private val api = Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build()
.create(OtelAPI::class.java)
fun getData() : Single<List<Otel>> {
return api.getOtels()
}
}
class OtelAPIService{
私有val BASE_URL=”http://www.ipucubilisim.com.tr/"
private val api=reformation.Builder()
.baseUrl(基本URL)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build()
.create(OtelAPI::class.java)
fun getData():单个{
返回api.getOtels()
}
}
FeedViewModel
class FeedViewModel : ViewModel() {
private val otelApiService = OtelAPIService()
private val disposable = CompositeDisposable()
val otels = MutableLiveData<List<Otel>>()
val otelError = MutableLiveData<Boolean>()
val otelLoading = MutableLiveData<Boolean>()
fun refreshData() {
getDataFromAPI()
}
private fun getDataFromAPI() {
otelLoading.value = true
disposable.add(
otelApiService.getData()
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribeWith(object : DisposableSingleObserver<List<Otel>>(){
override fun onSuccess(t: List<Otel>) {
otels.value = t
otelError.value = false
otelLoading.value = false}
override fun onError(e: Throwable) {
otelLoading.value = false
otelError.value = true
e.printStackTrace()}}))}}
<data>
<variable
name="otel"
type="com.berkancalikoglu.projem.model.Otel" />
</data>
<LinearLayout
..
<ImageView
android:id="@+id/imageView"
android:downloadUrl="@{otel.result.offers.Hotel.Details.extra.thumbnailImage}"> </ImageView>
<LinearLayout
..
<TextView
android:id="@+id/name"
android:text="@{otel.result.filters.price.max.name}""> </TextView>
class FeedViewModel:ViewModel(){
私有val otelApiService=otelApiService()
private val disposable=CompositeDisposable()
val-otels=MutableLiveData()
val-otelError=MutableLiveData()
val-otelLoading=MutableLiveData()
更新数据(){
getDataFromAPI()
}
私有函数getDataFromAPI(){
otelLoading.value=true
一次性。添加(
otelApiService.getData()
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribowith(对象:DisposableSingleObserver(){
成功时覆盖乐趣(t:列表){
otels.value=t
otelError.value=false
otelLoading.value=false}
覆盖有趣的错误(e:可丢弃){
otelLoading.value=false
otelError.value=真
e、 printStackTrace()}}}}}
item_otel.xml
class FeedViewModel : ViewModel() {
private val otelApiService = OtelAPIService()
private val disposable = CompositeDisposable()
val otels = MutableLiveData<List<Otel>>()
val otelError = MutableLiveData<Boolean>()
val otelLoading = MutableLiveData<Boolean>()
fun refreshData() {
getDataFromAPI()
}
private fun getDataFromAPI() {
otelLoading.value = true
disposable.add(
otelApiService.getData()
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribeWith(object : DisposableSingleObserver<List<Otel>>(){
override fun onSuccess(t: List<Otel>) {
otels.value = t
otelError.value = false
otelLoading.value = false}
override fun onError(e: Throwable) {
otelLoading.value = false
otelError.value = true
e.printStackTrace()}}))}}
<data>
<variable
name="otel"
type="com.berkancalikoglu.projem.model.Otel" />
</data>
<LinearLayout
..
<ImageView
android:id="@+id/imageView"
android:downloadUrl="@{otel.result.offers.Hotel.Details.extra.thumbnailImage}"> </ImageView>
<LinearLayout
..
<TextView
android:id="@+id/name"
android:text="@{otel.result.filters.price.max.name}""> </TextView>
您提供的json架构不完整。尽管我已将其转换为java pojo,并且您可以使用android studio将这些java类转换为Kotlin文件。请确保正确命名包和索引类文件。同时使用Gson序列化程序
-----------------------------------index.Address.java-----------------------------------
package index;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Address {
@SerializedName("address")
@Expose
private String address;
@SerializedName("zipCode")
@Expose
private Object zipCode;
@SerializedName("country")
@Expose
private Country country;
@SerializedName("countryCode")
@Expose
private String countryCode;
@SerializedName("town")
@Expose
private Town town;
@SerializedName("city")
@Expose
private City city;
/**
* No args constructor for use in serialization
*
*/
public Address() {
}
/**
*
* @param zipCode
* @param country
* @param address
* @param town
* @param city
* @param countryCode
*/
public Address(String address, Object zipCode, Country country, String countryCode, Town town, City city) {
super();
this.address = address;
this.zipCode = zipCode;
this.country = country;
this.countryCode = countryCode;
this.town = town;
this.city = city;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Object getZipCode() {
return zipCode;
}
public void setZipCode(Object zipCode) {
this.zipCode = zipCode;
}
public Country getCountry() {
return country;
}
public void setCountry(Country country) {
this.country = country;
}
public String getCountryCode() {
return countryCode;
}
public void setCountryCode(String countryCode) {
this.countryCode = countryCode;
}
public Town getTown() {
return town;
}
public void setTown(Town town) {
this.town = town;
}
public City getCity() {
return city;
}
public void setCity(City city) {
this.city = city;
}
}
-----------------------------------index.City.java-----------------------------------
package index;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class City {
@SerializedName("id")
@Expose
private Object id;
@SerializedName("name")
@Expose
private String name;
/**
* No args constructor for use in serialization
*
*/
public City() {
}
/**
*
* @param name
* @param id
*/
public City(Object id, String name) {
super();
this.id = id;
this.name = name;
}
public Object getId() {
return id;
}
public void setId(Object id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
-----------------------------------index.Country.java-----------------------------------
package index;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Country {
@SerializedName("code")
@Expose
private String code;
@SerializedName("name")
@Expose
private String name;
/**
* No args constructor for use in serialization
*
*/
public Country() {
}
/**
*
* @param code
* @param name
*/
public Country(String code, String name) {
super();
this.code = code;
this.name = name;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
-----------------------------------index.Details.java-----------------------------------
package index;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Details {
@SerializedName("id")
@Expose
private Object id;
@SerializedName("slug")
@Expose
private String slug;
@SerializedName("address")
@Expose
private Address address;
/**
* No args constructor for use in serialization
*
*/
public Details() {
}
/**
*
* @param address
* @param id
* @param slug
*/
public Details(Object id, String slug, Address address) {
super();
this.id = id;
this.slug = slug;
this.address = address;
}
public Object getId() {
return id;
}
public void setId(Object id) {
this.id = id;
}
public String getSlug() {
return slug;
}
public void setSlug(String slug) {
this.slug = slug;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}
-----------------------------------index.Hotel.java-----------------------------------
package index;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Hotel {
@SerializedName("id")
@Expose
private Integer id;
@SerializedName("details")
@Expose
private Details details;
/**
* No args constructor for use in serialization
*
*/
public Hotel() {
}
/**
*
* @param details
* @param id
*/
public Hotel(Integer id, Details details) {
super();
this.id = id;
this.details = details;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Details getDetails() {
return details;
}
public void setDetails(Details details) {
this.details = details;
}
}
-----------------------------------index.Index.java-----------------------------------
package index;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Index {
@SerializedName("responseCode")
@Expose
private Integer responseCode;
@SerializedName("errors")
@Expose
private Object errors;
@SerializedName("message")
@Expose
private Object message;
@SerializedName("result")
@Expose
private Result result;
/**
* No args constructor for use in serialization
*
*/
public Index() {
}
/**
*
* @param result
* @param message
* @param errors
* @param responseCode
*/
public Index(Integer responseCode, Object errors, Object message, Result result) {
super();
this.responseCode = responseCode;
this.errors = errors;
this.message = message;
this.result = result;
}
public Integer getResponseCode() {
return responseCode;
}
public void setResponseCode(Integer responseCode) {
this.responseCode = responseCode;
}
public Object getErrors() {
return errors;
}
public void setErrors(Object errors) {
this.errors = errors;
}
public Object getMessage() {
return message;
}
public void setMessage(Object message) {
this.message = message;
}
public Result getResult() {
return result;
}
public void setResult(Result result) {
this.result = result;
}
}
-----------------------------------index.Offers.java-----------------------------------
package index;
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Offers {
@SerializedName("hotels")
@Expose
private List<Hotel> hotels = null;
/**
* No args constructor for use in serialization
*
*/
public Offers() {
}
/**
*
* @param hotels
*/
public Offers(List<Hotel> hotels) {
super();
this.hotels = hotels;
}
public List<Hotel> getHotels() {
return hotels;
}
public void setHotels(List<Hotel> hotels) {
this.hotels = hotels;
}
}
-----------------------------------index.Result.java-----------------------------------
package index;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Result {
@SerializedName("requestId")
@Expose
private String requestId;
@SerializedName("offers")
@Expose
private Offers offers;
/**
* No args constructor for use in serialization
*
*/
public Result() {
}
/**
*
* @param offers
* @param requestId
*/
public Result(String requestId, Offers offers) {
super();
this.requestId = requestId;
this.offers = offers;
}
public String getRequestId() {
return requestId;
}
public void setRequestId(String requestId) {
this.requestId = requestId;
}
public Offers getOffers() {
return offers;
}
public void setOffers(Offers offers) {
this.offers = offers;
}
}
-----------------------------------index.Town.java-----------------------------------
package index;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Town {
@SerializedName("id")
@Expose
private Object id;
@SerializedName("name")
@Expose
private String name;
/**
* No args constructor for use in serialization
*
*/
public Town() {
}
/**
*
* @param name
* @param id
*/
public Town(Object id, String name) {
super();
this.id = id;
this.name = name;
}
public Object getId() {
return id;
}
public void setId(Object id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
------------------------------------index.Address.java-----------------------------------
包装指数;
导入com.google.gson.annotations.Expose;
导入com.google.gson.annotations.SerializedName;
公共课堂演讲{
@序列化名称(“地址”)
@暴露
私有字符串地址;
@SerializedName(“zipCode”)
@暴露
私有对象zipCode;
@序列化名称(“国家”)
@暴露
私人国家;
@SerializedName(“国家代码”)
@暴露
私有字符串国家代码;
@序列化名称(“城镇”)
@暴露
私人城镇;
@序列化名称(“城市”)
@暴露
私人城市;
/**
*没有用于序列化的args构造函数
*
*/
公共广播{
}
/**
*
*@param-zipCode
*@param国家
*@param地址
*@param镇
*@param市
*@param countryCode
*/
公共地址(字符串地址、对象zipCode、国家/地区、字符串国家/地区代码、城镇、城市){
超级();
this.address=地址;
this.zipCode=zipCode;
这个国家=国家;
this.countryCode=countryCode;
this.town=城镇;
this.city=城市;
}
公共字符串getAddress(){
回信地址;
}
公共无效设置地址(字符串地址){
this.address=地址;
}
公共对象getZipCode(){
返回zipCode;
}
public void setZipCode(对象zipCode){
this.zipCode=zipCode;
}
公共国家/地区{
返回国;
}
国家/地区(国家/地区){
这个国家=国家;
}
公共字符串getCountryCode(){
返回国家代码;
}
公共无效setCountryCode(字符串countryCode){
this.countryCode=countryCode;
}
公共城镇{
返回城镇;
}
城镇公共空间(城镇){
this.town=城镇;
}
公共城市{
回归城市;
}
公共城市(城市){
this.city=城市;
}
}
-----------------------------------index.City.java-----------------------------------
包装指数;
导入com.google.gson.annotations.Expose;
导入com.google.gson.annotations.SerializedName;
公营城市{
@序列化名称(“id”)
@暴露
私有对象id;
@序列化名称(“名称”)
@暴露
私有字符串名称;
/**
*没有用于序列化的args构造函数
*
*/
公共城市(){
}
/**
*
*@param name
*@param-id
*/
公共城市(对象id、字符串名称){
超级();
this.id=id;
this.name=名称;
}
公共对象getId(){
返回id;
}
公共无效集合id(对象id){
this.id=id;
}
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
}
-----------------------------------index.Country.java-----------------------------------
包装指数;
导入com.google.gson.annotations.Expose;
导入com.google.gson.annotations.SerializedName;
公营国家{
@序列化名称(“代码”)
@暴露
私有字符串码;
@序列化名称(“名称”)
@暴露
私有字符串名称;
/**
*没有用于序列化的args构造函数
*
*/
公共国家(){
}
/**
*
*@param代码
*@param name
*/
公共国家(字符串代码、字符串名称){
超级();
this.code=代码;
this.name=名称;
}
公共字符串getCode(){
返回码;
}
公共无效设置码(字符串码){
this.code=代码;
}
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
}
-----------------------------------index.Details.java-----------------------------------
包装指数;
导入com.google.gson.annotations.Expose;
导入com.google.gson.annotations.SerializedName;
公开课详情{
@连载